Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Sachin Yadav
Full Stack JavaScript Techdegree Student 430 PointsAnyone explain the logic behind this program.
void Reverse(); int main() { printf("Enter a sentence: "); Reverse(); return 0; } void Reverse() { char c; scanf("%c",&c); if( c != '\n') { Reverse(); printf("%c",c); } }
3 Answers
Gloria Dwomoh
13,116 PointsI will try to explain this to you as simple as I can because it is not that easy to explain with words. It is very confusing and easier to draw it on a whiteboard to explain it. First let me markdown your code in a way that makes it easy to read. I will also add some comments to it.
void Reverse(); // Declares the function called Reverse
int main() {
printf("Enter a sentence: ");
Reverse();
return 0;
} //end main()
void Reverse() { //creation of function its logic is written below
char c;
scanf("%c",&c);
if( c != '\n') {//If the character in c is NOT '\n' complete the tasks below
Reverse();
printf("%c",c);
}//end if
}//end Reverse()
The logic behind this program is recursion. In short, it takes a sentence from the user and reverses it. In depth it takes the each character from the input of the user and stores in in a new char variable called "c" with each recursion (that's because each call of scanf fetches the next character from the sentence). I said new character "c" because if you see the code each time Reverse() is called there is a new declaration of "c". When the new declaration of "c" happens it is given a new memory address. That is why is c variable is different because each stores its value on a new memory address. That continues to happen till it reaches the character "\n" ,which signifies new line (or Enter), so the function reverse stops being called since the "if condition" is not met and then all the it starts printing all the characters stored in the memory starting from the one before "\n" (if you remember due to continues calls of "Reverse()" the last printf's never got called so eventually due to recursion all of the printf not called till now start getting called.) Doing that, it reverses the sentence given for example...
I love ice cream.
to
.maerc eci evol I
I hope this helps :D
Sachin Yadav
Full Stack JavaScript Techdegree Student 430 PointsGloria,
Thanks again.
Some more queries.
- How can variable c store so many values in different instances?
Gloria Dwomoh
13,116 PointsThat's because each time you declare it, it gets a new memory address and the & before it in scanf makes sure that, that each character gets saved in its individual address of "c". Hence each time it gets declared...a new address gets created, and each character gets stored in another position of the memory. For example if you have live in a country that has 3 streets called with the same name such as... Gordon blue street. It seems like the same street but the area code, which here works as the address of each variable "c", differs hence that's what makes all the difference. So if the postman has
Gordon blue street with area code 1101
Gordon blue street with area code 1102
Gordon blue street with area code 1103
each time he is to send a letter to one of those. He knows exactly which letter belongs to which street, due to their area code :)
Sachin Yadav
Full Stack JavaScript Techdegree Student 430 PointsBeautiful. Thank you so much. Love.
Gloria Dwomoh
13,116 PointsYou are welcome :)
Sachin Yadav
Full Stack JavaScript Techdegree Student 430 PointsSachin Yadav
Full Stack JavaScript Techdegree Student 430 PointsThank you so much gloria.
I have a few doubts still.
-Can we not use anything other than\n? Like \0 'null character'
-I'm still not clear if there's a Reverse func. predefined in lib.
Gloria Dwomoh
13,116 PointsGloria Dwomoh
13,116 Points-Can we not use anything other than\n? Like \0 'null character'
You can use anything. You can even use "e" it would start printing the characters that precede "e" as soon as it meets its first "e" character. The point is that character you use should be in the sentence. If you use a character that does not exist there is a possibility that it might never terminate the function or terminate it and give you some sort of error.
-I'm still not clear if there's a Reverse func. predefined in lib.
There, Reverse function is not predefined. You created it below. That is why I commented that it gets created. If it was predefined in a lib of C you'd have to import the library of it first. I guess the code you posted somewhere above it, it has
import <stdio.h>and only that.
That library helps with functions that use the input and output stream such as printf and scanf. Scanf and printf are the only functions in this code that are predefined and declared in the stdio header file. If you could open that header file you'd see these functions declared in it.