3/20/2016

Program to Reverse a String in C | Using Loop, Recursion, and Strrev()

C Program to Reverse a String in C programming language. We are going to solve reverse words in a string using while loop, using strrev predefine function, reverse a string using pointers, and using recursion in C language.

Reverse a String- Table of Content


  • Reverse a String Logic Explanation
  • Words Reverse in a String Using While Loop C Program
  • C Program to Reverse a String Using Strrev Function
  • Reverse a String Using Recursion in C
  • Output of Code

Reverse a String Logic Explanation


we can reverse a string without using the library function and strrev(). In this program, I am going to reverse the string with and without using strrev(). Firstly, we need to find the length of the string.

After finding the length of the string run a loop from the last index to string the first index, and in each iteration decrease the last index by 1. So we can reverse the full string, this is a similar problem to reverse the sentence, and we can solve this problem by using a strrev() library function.

There are many ways to solve this problem, we can solve this problem using Using Pointers, strrev(), and Recursion in C programming language.

Special Case: There is an special case for this problem we need to know about it. Let's take an example of it. As we can see that the below program has more than 1 spaces between the words so our output should be according to spaces are given in a String.

Input Format


Programming   With  Basics Website

Output Format


etisbeW scisaB  htiW   gnimmargorP


Instead, for using library function always solve the problem with your own logic, in this way your logic building will be implemented

Reverse a String in C Using While Loop


#include<stdio.h>
#include<string.h>

void main()
{
 /*Visit for more- https://www.programmingwithbasics.com */

   char str[100], temp;
   int i, j = 0;

   printf("Enter The String: ");
   gets(str);

   i = 0;
   j = strlen(str) - 1;

   while (i < j) 
   {
      temp = str[i];
      str[i] = str[j];
      str[j] = temp;
      i++;
      j--;
   }

   printf("\nReverse Words in a String Is: %s\n\n", str);
   
   return (0);
}

C Program to Reverse a String Using Function strrev()


#include<stdio.h>
#include<string.h>

void main()
{
 /*Visit for more- https://www.programmingwithbasics.com */

   char str[100], temp;
   int i, j = 0;

   printf("Enter The String: ");
   gets(str);

   strrev(str);

   printf("\nReverse Words in a String Is: %s\n\n", str);
   
   return (0);
}


Program to Reverse a String Using Recursion


#include <stdio.h>
#include <string.h>
 
void revstr(char*, int, int);
 
int main()
{
    char str[100];

printf("Enter The String: "); 
    gets(str);
 
    revstr(str, 0, strlen(str)-1);
 
    printf("%s\n", str);
 
    return 0;
}
 
void revstr(char *z, int start, int end)
{
    char ch;
 
    if (start >= end)
      return;
 
    ch = *(z+start);
    *(z+start) = *(z+end);
    *(z+end) = ch;
 
    revstr(z, ++start, --end);
}

Output of Code


Reverse a String in C

Similar programs

  1. Number Is Armstrong Or Not Using If/Else Statements
  2. Find Character Is Vowel Or Not Using If/Else Statements
  3. Check Number Is Even Or Odd Using If/Else Statements
  4. Prime Or Not Using If/Else Statements
  5. C++ Program To Check Character Is Uppercase, Lowercase Alphabet or A Digit or A Special Symbol Using If/Else Statements
  6. Program to Find GCD of Two Numbers Using For Loop
  7. Find LCM of two Numbers Using while Loop
  8. Reverse a Number Using While Loop
  9. Check Whether a Number is A Palindrome or Not using While Loop
  10. C Program to Count Number of Digits of an Integer Using While Loop

No comments:

Post a Comment