This C Program reverse the string using both recursion and iteration.
Here is source code of the C Program to reverse the string using both recursion and iteration. 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 Both Recursion and Iteration
*/
#include <stdio.h>
#include <string.h>
/* Function Prototype */
void disp_str1_rec(char *);
void main()
{
char str1[100], *ptr;
int len1 = 0, i;
char ch;
printf("Enter the string:\n");
scanf("%[^\n]s", str1);
ptr = str1;
len1 = strlen(str1);
printf("Using iteration:\n");
for (i = len1 - 1; i >= 0;i--) /* Iterative loop */
{
ch = str1[i];
printf("%c", ch);
}
printf("Using recurssion:\n");
disp_str1_rec(ptr);
}
/* Code to reverse the string using Recursion */
void disp_str1_rec(char *stng)
{
char ch;
if (*stng != '\0')
{
ch = *stng;
stng++;
disp_str1_rec(stng);
printf("%c", ch);
}
else
return;
}
$ cc string21.c $ a.out Enter the string: welcome to sanfoundry's c programming class Using iteration: ssalc gnimmargorp c s'yrdnuofnas ot emoclew Using recurssion: ssalc gnimmargorp c s'yrdnuofnas ot emoclewi
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at programming examples on all topics, go to C Programming Examples.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Practice BCA MCQs
- Practice Computer Science MCQs
- Apply for C Internship
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos