1

i need to write a recursion function thats gets a string and then print only letters (captial & small) from the end to start

for exemple:
str={"abc123#@!456DEF&^65gHj"}
Reducing(str);
output: jHgFEDcba .

but im stuck on the part of how to get the function go on to the next char all i can do is print the last char and thats it im stuck... please help

here's my code:

void Reducing(char str[])
{
    if (str[0] == '\0')
        return ;

    if ((str[strlen(str)-1] <= 122 && str[strlen(str)-1] >= 97) || (str[strlen(str)-1] <= 90 && str[strlen(str)-1] >= 65))
        putchar(str[strlen(str)-1]);

    Reducing(str+(strlen(str)));
}
5
  • 2
    Can you make a simpler version, which is just reversing the string as is? Then modify it to filter. And hint: It should not be tail recursion (well, it can, but you loose the point). Commented May 26, 2017 at 14:58
  • 1
    Don't attempt to compute strlen on each iteration. Just move one character up on each iteration, then unwind. Commented May 26, 2017 at 14:59
  • Using isalpha() would be cleaner and remove the magic numbers. Commented May 26, 2017 at 15:01
  • i dont understand you how to move one char???? Commented May 26, 2017 at 15:07
  • I suggest you to try Robozzle :) It makes a heavy usage of head recursion. Commented May 26, 2017 at 15:08

3 Answers 3

4

I believe the whole point of the exercise is to move down the string one character per recursive step, until you hit the end, and then print the current character on the way back (after the recursive call). You are not supposed to use strlen.

A string is a sequence of characters in memory terminated by a zero (or NUL) character. Providing the address of the first character in the string allows you to find all of the other characters by adding an offset to the address. A string can also be thought of like a snake with a head (the first character), and a tail (the remaining characters). The head is the single character located at the address of the string. The tail is the string located at the address plus 1.

In order to print a string backwards, if the string is empty (its head is NUL), then we don't have to do anything. Otherwise, if we print the tail backwards by recursively calling the backwards print function, and then print the head character, then we accomplish our goal.

Sign up to request clarification or add additional context in comments.

6 Comments

so how am i supposed to move down the string one char??
@pat. I deleted my answer. OP needs a walking lesson, not a fishing lesson.
str is a pointer to a contiguous array of characters in memory. Granted, it is declared as an array, but as an argument to a function, it is really a pointer. You could change the type from char[] to char* to make it more obvious.
i dont understand you guys please be easy with me im new in that
@monkey.D.Ganga You need to know a basic terminology in order to properly communicate this stuff. If you don't have it you might need to make few steps back.
|
1

How about using just str[0] instead of str[strlen(str) - 1] and pass str + 1 for the recursion:

void Reducing(char str[])
{
    if (str[0] == '\0')
        return ;

    if ((str[0] <= 122 && str[0] >= 97) ||
            (str[0] <= 90 && str[0] >= 65))
        putchar(str[0]);

    Reducing(str + 1);
}

3 Comments

thats whats i did at first try but it prints frim start to end and i want it to do the opposite, thats why i try to use the "str[strlen(str)-1]"
@monkey.D.Ganga Change the order of the function then
goddamn im so noob that i dident think about it thanks alot it works!! and now i know what i did worng and how to fix it next time =)
1

First of all, the interface you typically use to take a string to read from it is:

void reducing(const char*);

The const is important here if you want to call your function on string literals (which you typically want).

Second, the c standard library provides functions such as isalpha() in the ctype header to check if a char belongs to certain standard categories.

And last but not least, what you really want to do is look at the next character, so advance the pointer by one and call yourself again until you read a '\0'.

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.