C

Reverse a String In C Using Recursion

In this tutorial, we are going to see how to reverse a string in C using recursion. For example, if a user enters the string “StackHowTo”, it will be “oTwoHkcatS” when reversed. A string that stays the same when reversed is a string named Palindrome.

There are four ways to reverse a string in C, by using for loop, pointers, recursion, or by strrev function.
100-multiple-choice-questions-in-c-programming100 Multiple Choice Questions In C Programming – Part 1This collection of 100 Multiple Choice Questions and Answers (MCQs) In C Programming : Quizzes & Practice Tests with Answer focuses on “C Programming”.  …Read More

Reverse a String In C Using Recursion
#include <stdio.h>
 
void reverse(char *p, int start, int end)
{
   char ch;
   if (start >= end)
      return;
 
   ch = *(p+start);
   *(p+start) = *(p+end);
   *(p+end) = ch;
   reverse(p, ++start, --end);
}
 
int main()
{
   char str[100];
   printf(" Enter a string : ");
   gets(str);
   reverse(str, 0, strlen(str)-1);
   printf(" String after reversing it: %s", str);
   return 0;
}

Output:

 Enter a string :  StackHowTo
 String after reversing it: oTwoHkcatS

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *