Users Online
· Guests Online: 115
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
C Program to Reverse the String using Recursion
This C Program uses recursive function & reverses the string entered by user in the same memory location. Eg: “program” will be reversed to “margorp”
Here is the source code of the C program to reverse a string. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Reverse the String using Recursion
*/
#include <stdio.h>
#include <string.h>
void reverse(char [], int, int);
int main()
{
char str1[20];
int size;
printf("Enter a string to reverse: ");
scanf("%s", str1);
size = strlen(str1);
reverse(str1, 0, size - 1);
printf("The string after reversing is: %s\n", str1);
return 0;
}
void reverse(char str1[], int index, int size)
{
char temp;
temp = str1[index];
str1[index] = str1[size - index];
str1[size - index] = temp;
if (index == size / 2)
{
return;
}
reverse(str1, index + 1, size);
}
$ cc pgm12.c
$ a.out
Enter a string to reverse: malayalam
The string after reversing is: malayalam
$ a.out
Enter a string to reverse: cprogramming
The string after reversing is: gnimmargorpc
Here is the source code of the C program to reverse a string. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Reverse the String using Recursion
*/
#include <stdio.h>
#include <string.h>
void reverse(char [], int, int);
int main()
{
char str1[20];
int size;
printf("Enter a string to reverse: ");
scanf("%s", str1);
size = strlen(str1);
reverse(str1, 0, size - 1);
printf("The string after reversing is: %s\n", str1);
return 0;
}
void reverse(char str1[], int index, int size)
{
char temp;
temp = str1[index];
str1[index] = str1[size - index];
str1[size - index] = temp;
if (index == size / 2)
{
return;
}
reverse(str1, index + 1, size);
}
$ cc pgm12.c
$ a.out
Enter a string to reverse: malayalam
The string after reversing is: malayalam
$ a.out
Enter a string to reverse: cprogramming
The string after reversing is: gnimmargorpc
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.