In C programming, string reversing is a crucial topic. There are several methods for reversing a string, each of which has a distinct logic. In this chapter, you'll learn how to manually reverse a string using two separate principles, and reasoning without utilizing any preset functions.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Example

// A Simple C++ program to reverse a string

#include <bits/stdc++.h>

using namespace std;

// Function to reverse a string

void reverseStr(string& str)

{

int n = str.length();

// Swap character starting from two

// corners

for (int i = 0; i < n / 2; i++)

swap(str[i], str[n - i - 1]);

}

// Driver program

int main()

{

string str = "geeksforgeeks";

reverseStr(str);

cout << str;

return 0;

}

Output

C_Program_to_Reverse_String_1

What is the Meaning of Reversing a String?

When the reverse of a string algorithm is applied in that particular input string, the string sent in a certain sequence by the user to your software is completely reversed. For example, if the string is "hello", the output will be "olleh".

This concept can be used to check the palindrome. A palindrome is a string that remains the same when reversed. So, a palindrome string will have the same value even after we reverse it. 

C program uses different ways to reverse a string entered by the user. A given string can be reversed in the C language by using strrev function,without strrev, recursion, pointers, using another string, or displaying it in reverse order. The below given example shows how to reverse a string using two of these methods.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

C Program To Reverse A String Using Recursion

Example

#include<stdio.h>

#include<string.h>

int main(void)

{

char mystrg[60];

int leng, g;

// Printing the program name and what the program will do

printf("Program in C for reversing a given string \n ");

printf("Please insert the string you want to reverse: ");

    // fetch the input string from the user

scanf( "%s", mystrg );

// This will find the length of your string with the help of strlen() function of string.h header file

leng = strlen(mystrg);

// iterate through each and every character of the string for printing it backwards or reverse direction

for(g = leng - 1; g >= 0; g--) {

     printf("%c", mystrg[g]);

}

return 0;

}

Output

C_Program_to_Reverse_String_2

Preparing Your Blockchain Career for 2024

Free Webinar | 5 Dec, Tuesday | 9 PM ISTRegister Now
Preparing Your Blockchain Career for 2024

C program to Reverse a String Using strrev() Function

Here,in this example, we take input from the user using gets() function.The string is then reversed by a predefined function strrev(). 

Example

#include <stdio.h>

#include <string.h>

int main()

{

   char s[100];

   printf("Enter a string to reverse\n");

   gets(s);

   strrev(s);

   printf("Reverse of the string: %s\n", s);

   return 0;

}

Output

C_Program_to_Reverse_String_3.

A C program to Reverse a String Without Using strrev()

The program uses For Loop to iterate each character in the given string, and saves the characters in reverse order. Also, we are using the temp variable to change the location of the character like a swapping technique.

Example

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main(){

   char string[20],temp;

   int i,length;

   printf("Enter String : ");

   scanf("%s",string);

   length=strlen(string)-1;

   for(i=0;i<strlen(string)/2;i++){

      temp=string[i];

      string[i]=string[length];

      string[length--]=temp;

   }

   printf("\nReverse string :%s",string);

   getch();

}

Output

C_Program_to_Reverse_String_4

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

A Program to Reverse a String With Pointers

This is the same as using recursion to reverse a string except that we are using Pointers to separate the logic from the main program.

Example

#include <stdio.h>

#include <string.h>

// Function to reverse the string

// using pointers

void reverseString(char* str)

{

int l, i;

char *begin_ptr, *end_ptr, ch;

// Get the length of the string

l = strlen(str);

// Set the begin_ptr and end_ptr

// initially to start of string

begin_ptr = str;

end_ptr = str;

// Move the end_ptr to the last character

for (i = 0; i < l - 1; i++)

end_ptr++;

// Swap the char from start and end

// index using begin_ptr and end_ptr

for (i = 0; i < l / 2; i++) {

// swap character

ch = *end_ptr;

*end_ptr = *begin_ptr;

*begin_ptr = ch;

// update pointers positions

begin_ptr++;

end_ptr--;

}

}

// Driver code

int main()

{

// Get the string

char str[100] = "MeghaBali";

printf("Enter a string: %s\n", str);

// Reverse the string

reverseString(str);

// Print the result

printf("Reverse of the string: %s\n", str);

return 0;

}

Output

C_Program_to_Reverse_String_5

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

A Program to Reverse a String Using Another String

In this example, we first accept the user's input and then determine the length of the string. To compute the length, we execute a loop from the beginning of the character array until we find a null character ('0'), increasing the count variable with each iteration. Because the array starts at zero, we assign one less than it to the j. Later, we just copy the characters from the conclusion,  one by one, from the old character array to a new character array.

Example

#include <stdio.h>

int main()

{

  char str[1000], rev[1000];

  int i, j, count = 0;

  scanf("%s", str);

  printf("\nString Before Reverse: %s", str);

  //finding the length of the string

  while (str[count] != '\0')

  {

    count++;

  }

  j = count - 1;

  //reversing the string by swapping

  for (i = 0; i < count; i++)

  {

    rev[i] = str[j];

    j--;

  }

  printf("\nString After Reverse: %s", rev);

}

Output

C_Program_to_Reverse_String_6

Become a Certified UI UX Expert in Just 5 Months!

UMass Amherst UI UX BootcampExplore Program
Become a Certified UI UX Expert in Just 5 Months!

C Program to Reverse a String by Displaying It in Reverse Order

Example

#include <stdio.h>

#include <string.h>

void main()

{

   char str1[100], tmp;

   int l, lind, rind,i;

       printf("\n\nPrint a string in reverse order:\n ");

       printf("-------------------------------------\n");

   printf("Input a string to reverse : ");

   scanf("%s", str1);

   l = strlen(str1);

   lind = 0;

   rind = l-1;    

for(i=lind;i<rind;i++)

       {

       tmp = str1[i];

       str1[i] = str1[rind];

       str1[rind] = tmp;

       rind--;

   } 

   printf("Reversed string is: %s\n\n", str1);

}

Output

C_Program_to_Reverse_String_7

Master front-end and back-end technologies and advanced aspects in our Post Graduate Program in Full Stack Web Development. Unleash your career as an expert full stack developer. Get in touch with us NOW!

Conclusion

We hope you have understood the topic and now have a thorough grip on the concept. Enroll now to  the Simplilearn's Full Stack Development course for better understanding of coding for the complex outputs. And if you are looking to upgrade your skill or even thinking of pursuing a new course in this field,enroll in Simplilearn's skill up course and explore and learn in-demand skills on your schedule.