recursively print character using if and while

what is the different of using while and if here?
why if i use "if" it produce correct result?
but "while" doesnt?
thankyou!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <stdio.h>
#include <stdlib.h>
void back(char*k){
if(*k) // works
//while(*k) ->not work??
    back(++k);
printf("%c",*k);
}
int main()
{
    char k[]="hellomynameis";
    back(k);
    printf("Hello world!\n");
    return 0;
}


is there possible way to do same thing in c++ but using iterator?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
using namespace std;
void backk(string a){
string::iterator itr;
for(itr=a.begin();itr!=a.end();itr++){
  if(*itr)
backk(++itr);
cout<<a; --> is this possible?

//simple and fast solution 
for(itr=a.end();itr!=a.begin();itr--){
    if(*itr)
    cout<<*itr;

}


}
   
}
return
}

int main()
{   string a("hello my name is");
    backk(a);
    cout << "Hello world!" << endl;
    return 0;
}
You can do it with while loop too ;)
1
2
3
4
5
void back(char*k){
    while(*++k);
    while(*--k)
        printf("%c",*k);
}


The point of using a recursive function was not because that was the only way but so you get used to how recursion works.

By the way you can use reverse_iterators
1
2
for ( std::string::reverse_iterator itr=a.rbegin(); itr!=a.rend(); itr++)
        printf("%c",*it);


Or your for-loop which is fine too.
thanks!! but i really want to know why "while" not working here?
i mean it suppose to get out from the loop when it not satisfy the condition (*k)
but why "if" work and while not?
while only excute printf once and go to the while loop again(?)
i understood your code recur down after it reach "\0"

First you need to understand how exactly the recursion is working with the if-statement.

1
2
3
4
5
void back(char*k){
if(*k) 
    back(++k);
printf("%c",*k);
}


The recursive case is when the (if statement) evaluates to true: back(++k);
There's no base case but the recursion is guaranteed to stop when it hits NULL.

We have a pointer to a string, we want to recursively print each character (in reverse) until we reach '\0' which is the end of the string.

if(*k) will evaluate to true when not '\0'

So when the function is called,
The function checks if it has reached the end of the string. If not, then it calls itself with a pointer pointed to the next location of the same string.

(Remember that when the function calls itself, it pauses and starts a new instance of itself. After the instance has finished, it continues.)

This goes on until we reach the '\0'. Now the last instance of the function does not evaluate the if statement, so it goes to the next line without calling itself, which would be the print.

After this instance is done printing, it has done its job, it returns control to the function instance that called it. Now this instance prints the character that it is pointing to and this goes on until all instances have returned.

That is what gives you your output.


Now that you know what makes the recursion work, try to think why using a while-loop in the way you mentioned, wouldn't work. ;)

@Grime thankyou for the explanation!
while forces the string to go over again and again until NULL is reached, so it will not get out from the loop until it reach "\0" and
for example string "GH\0"

1
2
3
4
5
void back(char*k){
while(*k) ->     
    back(++k);
printf("%c",*k);
}


in stack after call ---> H -->\0 --->\0
it reached \0 and printf \0 and return to second back(++k) call, cause while(*\0) to execute
and printf \0
and return to first back(++k) call , but because of the call to function it will execute while loop so it will execute back(++k) which is \0, then produce \0
and last produce \0

so in conclusion it will execute string until NULL is reach

the different from "if" is if will control recursion until *k false and will execute next line after each recursion call , am i right?
Basically this is an infinite loop:
while(*k) (when *k is not NULL)

so if you replaced back(++k); with printf("still in loop");
as so:

1
2
3
4
5
void back(char*k){
while(*k) 
    printf("still in loop");
printf("%c",*k);
}


You will notice that the loop is never left. Similarly instead of the printing statement if there were back(++k) it would call an instance of itself which would call an instance of itself and so on until there is a NULL. The instance that calls the instance which found the null would be stuck at constantly calling the function that found the NULL.

Topic archived. No new replies allowed.