I'm beginner and try to understand how recursion works.
what is the different of using while and if int the code?
why if i use "if" it produce correct result?
but "while" doesn't?
while(*k) ,when it reach "\0" then it print only once then it goes back to while loop again
thank you so much for the help!!
#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?
#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;
}
}
}
int main()
{
string a("hello my name is");
backk(a);
cout << "Hello world!" << endl;
return 0;
}