Click here to Skip to main content
15,911,785 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//Reverse a string using recursion
#include<stdio.h>
#include<string.h>
void fun(char *str);
void main()
{
char str[30];
static int i;
printf("Enter a string:");
gets(str);
fun(&str[0]);
}
void fun(char *str)
{
int len;
len=strlen(str);
if(len)
{
fun(++str);
}
printf("\n %c",*str);

}

As per the output the printf() is printing the values after ++str, hence omitting the first character of a string. Ex- "Help me" is string then output is: em ple.

What I have tried:

But if i follow this simple program, then the output is 0123 and it not omitting the first integer.
void main()
{
abc(3);
}
void abc(int a)
{
if(a)
{
abc(a-1);
}
printf("%d",a);
}

Suggestions please. Thanks in advance.
Posted
Updated 1-Aug-18 21:36pm

The "++str" is the cause of that. Placing the ++ first means it is pre-incrementing. The pointer is incremented first and then it is passed to fun. You might find that post-incrementing is what you want here. That would be "str++". This would increment str after its value is pushed on the stack.

Personally, I prefer neither of those constructs because they can be easily overlooked. I would write that as two independent statements - first call the function and then increment the pointer.
 
Share this answer
 
Comments
Member 13922884 2-Aug-18 7:17am    
Thank you. It works now.
Quote:
The program is not printing the first character of the string while using recursion function.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Member 13922884 2-Aug-18 7:16am    
Thank you for your suggestion. I tried it.
As pointed out by Rick ++str is the mistake. The trick is passing (str+1) to the recursive call, try:

#include<stdio.h>

#define MAXLEN 30

void print_reversed(const char *str);

int main()
{
  char str[MAXLEN];
  printf("Enter a string:\n");
  fgets(str, MAXLEN, stdin);
  print_reversed(str);
  printf("\n");
  return 0;
}

void print_reversed(const char *str)
{
  if ( *str=='\n' || *str=='\0') return;
  print_reversed( str + 1);
  printf("%c",*str);
}
 
Share this answer
 
Comments
Member 13922884 2-Aug-18 7:16am    
Thanks
CPallini 2-Aug-18 7:27am    
You are welcome.
Member 13922884 5-Aug-18 8:15am    
I tried using fgets function today, not in this program but in some other to get data from the user. But, i found that fgets() is used to read data from a file. How come are you using it to get the data from the user using fgets().
CPallini 5-Aug-18 13:21pm    
(Console) user input is a special file (always open), the corrensponding file pointer is called stdin. See, for instance:
https://www.tutorialspoint.com/cprogramming/c_input_output.htm
Member 13922884 5-Aug-18 14:58pm    
Ok. Learned something new. Thanks a ton !!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900