Write a program to reverse any string using recursion (Maximum 15 steps)
Eg: Input : Bjarne Stroustrup developed C++
Output : ++C depoleved purtsuortS enrajB
This is one of the common question asked in technical contests and interviews.!! it checks ur knowledge about recursion and string. We recursively calls itself until encountering endl character. This fragment plays the vital role:
{
if (current character != endl) process( next caharacter);
print(current character);
}
My code:
#include<stdio.h>
char line[20];
void print(int i)
{
if(line[i]!='\0')
print(i+1);
printf("%c",line[i]);
}
main()
{
printf("Enter a string..: ");
gets(line);
print(0);
getch();
}
I hope this code was useful for u!!
Have a better program for ths prblm??
Feel free to comment it here..!! :):):)
Eg: Input : Bjarne Stroustrup developed C++
Output : ++C depoleved purtsuortS enrajB
This is one of the common question asked in technical contests and interviews.!! it checks ur knowledge about recursion and string. We recursively calls itself until encountering endl character. This fragment plays the vital role:
{
if (current character != endl) process( next caharacter);
print(current character);
}
My code:
#include<stdio.h>
char line[20];
void print(int i)
{
if(line[i]!='\0')
print(i+1);
printf("%c",line[i]);
}
main()
{
printf("Enter a string..: ");
gets(line);
print(0);
getch();
}
I hope this code was useful for u!!
Have a better program for ths prblm??
Feel free to comment it here..!! :):):)