Write a C program to reverse a string using recursion.
#include <string.h>
/* reverse the given string using recursion */
void strrev(char *str, int len) {
if (len < 0)
return;
printf("%c", str[len]);
strrev(str, len - 1);
}
int main() {
char str[100];
int len;
/* get the input string from the user */
printf("Enter ur string:\n");
fgets(str, 100, stdin);
str[strlen(str) - 1] = '\0';
/* find the length of the given string */
len = strlen(str);
/* find string reverse */
strrev(str, len - 1);
printf("\n");
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter ur string:
Hello World
dlroW olleH
Enter ur string:
Hello World
dlroW olleH
No comments:
Post a Comment