You have many flaws in your code.
[EDIT] Now that you have replaced
gets
by
puts
, items 1...3 do not apply anymore to your example - they are nonetheless to be considered in general.
[/EDIT]
1.
don't use gets[
^]: use
fgets(...)
instead
gets(str);
fgets(buffer, BUF_SIZE-1, STDIN);
2. don't use a string literal as buffer to store data:
char* p = "this is dummy content";
gets(p);
const int BUF_SIZE = 50;
char buffer[BUF_SIZE];
fgets(buffer, BUF_SIZE-1, STDIN);
buffer[BUF_SIZE-1] = '\0';
3. Take that magic reading from stdin out of the function. That does not belong there.
4. How do you know the
num
is not beyond the end of the
str
?
5. I strongly suggest to add blocks instead of single line in an
if
...! Especially important for my hint below.
if(len>=num)
printRevFromTo(str,num+1,len);
if (len >= num) {
printRevFromTo(str, num+1, len);
}
6. This does not print reverse, it prints forward, as if you would
printf("%s", str);
(without recursion, of course).
Hint: once you have fixed the above flaws, to get the reverse printing, move the
printf(...)
a bit further down... ;-) Play with it to learn the effect of head and tail recursion!
Cheers
Andi