0

The task is to write a recursive function which returns the last number (ASCII) in a char array. If there is no number in a given array it should return 0.

Edit

In this case the function should only return '4' from array a, because it is the last digit in that array and in array b it should return '0', because there is no digit at all in that array.

char rek(char a[], int i){
    int j = i;
    char tmp = '0';
    if((a[j] >= '0') && (a[j] <= '9')){
            tmp = a[j];
    }
    while(a[j] != '\0'){
        rek(a, i+1);
    }
    return tmp;
}

int main(){

    char a[7] = {'a','b','1','c','4','n','\0'};
    char b[7] = {'a','b','c','c','a','n','\0'};

    printf("Letzte Ziffer: %c \n", rek(a, 0));
    printf("Letzte Ziffer: %c", rek(b, 0));
    getchar();
    return 0;
}

I understand that everytime my function is recursively called tmp is set to '0' which is not what i want.

Somehow i can't figure out, how the function can call itself with the next element in the array to look at, without losing the set tmp value.

I also don't want to make tmp a global variable.

Furthermore what was unexpected to me, it outputs 'c' as result in bot lines. I thought my if-statement should prevent that.

2
  • "...which returns the last number..." - by "last number" you mean the last digit character ?? Related: if your supposed-to-be trivial recursive function has a hard loop (while/for) it's probably wrong. Think about how the recursion is the loop; not the non-recursive part of the function. Commented Apr 22, 2019 at 8:59
  • I edited the question, since it was not well asked by me. Commented Apr 22, 2019 at 9:06

3 Answers 3

2

Call the function recursively until you reach \0.

And consider return value of recursive call.

   char rek(char a[]){
        char ret = '0';

        if (*a != '\0')
           ret = rek(a+1);

        if((*a >= '0') && (*a <= '9') && ret == '0'){
                ret = *a;
        }

        return ret ;
    }
Sign up to request clarification or add additional context in comments.

7 Comments

Thak you for your answer it works, now i am trying to understand what happens. The function is called and first of all it checks if it is the end of the array. If not we call the function again with the next element. This goes on and on until the end is reached. If the end is reached the second if statement checks if it is a digit. But how does it work, that it only returns the last digit. I dont really get this.
ret == '0' or `0'? (the description wasn't clear)
@AnttiHaapala Good catch :D. But the return type is char. Any suggestion?
0 is a possible value for char. Or maybe it does need to return '0', cannot tell.
When we are into nitpicking, the array index is redundant. If the caller want to start somewhere else the offset could be added to the pointer. It's not unreasonable to have an offset as a argument, but it's makes the recursion pointless but since this is a convoluted use of recursion it doesn't matter really.
|
2

Whenever using recursion, try to implement tail recursion because tail recursive functions considered better than non tail recursive functions as tail-recursion can be optimized by compiler.

Using tail recursion, you can do:

#include <stdio.h>

char rek(char a[], char i){
    if (*a == '\0')
        return i;

    if ((*a >= '0') && (*a <= '9'))
        i = *a;

    return rek(++a, i);
}

int main(){

    char a[7] = {'a','b','1','c','4','n','\0'};
    char b[7] = {'a','b','c','c','a','n','\0'};

    printf("Letzte Ziffer: %c \n", rek(a, '0'));
    printf("Letzte Ziffer: %c", rek(b, '0'));
    getchar();
    return 0;
}

Comments

1

You need to understand recursion. Try to read abit about it. There are no need for the while loop. Also the array index is redundant since it's recursive. Then try to understand this:

#include <stdio.h>

char rek(char a[]){
    if(*a != 0){ /* If we are not at the end of the string ... */
        char tmp = rek(a + 1); /*... do a recursive call at the next char.*/
        if(tmp) return tmp; /* If there are numbers later in the string return that. */
    }
    if((*a >= '0') && (*a <= '9'))
        return *a;
    else
        return 0;
}

int main(){

    char a[7] = {'a','b','1','c','4','n','\0'};
    char b[7] = {'a','b','c','c','a','n','\0'};

    printf("Letzte Ziffer: %c \n", rek(a));
    printf("Letzte Ziffer: %c", rek(b));

    return 0;
}

You should also note that using recursion for this problem is only for pedagogic reasons. This problem is better solved with a simple loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.