How to Reverse a String in C

How to Reverse a String in C Language with Examples

In this article, I am going to discuss How to Reverse a String in C Language with Examples. Please read our previous article where we discussed How to Validate a String in C Language with Examples.

How to Reverse a String in C Language?

In this article, we will see how to reverse a String.

How to Reverse a String in C Language with Examples

Here we have taken an example. We have taken a string ‘Ruby’. We want to reverse this string. The reverse string will be: ‘ybuR’. For reversing, there is more than one method so we will show you the 1st method for reversing a string.

In 1st method, we will take another array and copy the reverse form of that original string in this array. So, the original will remain the same and we will get the reverse version of that string in another array. We have to add ‘\0’ at the end of another array. So, it becomes a string so let us see how we can reverse it. For reversing, we have to reach the end of a string that is the last alphabet:

How to Reverse a String in C Language with Examples

So once we are on the last alphabet, we can start copying those elements in another array in reverse order as:

How to Reverse a String in C Language

So, it is just like reverse copying a string. We will take ‘j’ as the index pointer and we will go on moving this until we reach ‘\0’.

How to Reverse a String in C Language

So ‘j’ is pointing to the last element. We have to copy that element in another array:

How to Reverse a String in C

We have to decrement j to the value of -1. And if j is on -1 then we have to stop there. In this way, we will copy all the alphabets from the original array to our extra array in reversing order. And when we copy all the alphabets or point on ‘\0’.

How to Reverse a String in C

Now, this becomes a string. It was the array of characters but as we have added ‘\0’ then it has become a string. At last, we have to print this reverse form of string. Below is the code for the above procedure:

Program for Reversing a String using Extra Array in C language:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
        char B[] = “Ruby”;
        int C[5];
        int i, j;
        for (i = 0; B[i] != ‘\0’; i++)
       {
            // no code here
       }
       i = i – 1;
       for (j = 0; i >= 0; i–, j++)
       {
            C[j] = B[i];
       }
       C[j] = ‘\0’;
       printf (“Original String is \”%s\”\n”, B);
       printf (“Original String is \””);
      for (int k = 0; k < 4; k++)
      {
           printf (“%c”, C[k]);
      }
      printf (“\””);
      return 0;
}

Output:

Reverse a String Code using Extra Array in C language

Reverse a String without using Extra Array in C Language:

Now let us look at the 2nd method for reversing a string and in this method, we don’t require any extra array. Same string, we will modify, and one more thing we should know is that in some C / C++ latest compiler, strings are not mutable means they cannot change.

Instead of an array if we take a pointer then those strings cannot be modified. So, make sure that you are taking a mutable string. In C / C++ declare a string with an array of characters. So, we will modify the same string. Now let us come to the procedure.

Reverse a String without using Extra Array in C Language

We will swap the character with a suitable position like The 4th letter with the 1st letter and the 2nd letter with the 3rd letter. In this way, we will exchange the characters of a string.

Program to Reverse a String without extra Array in C Language:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
         char B[] = “Ruby”;
         char t;
         int i, j;
         printf (“Original String is \”%s\”\n”, B);
        for (j = 0; B[j] != ‘\0’; j++)
        {
              // no code here
        }
        j = j – 1;
        for (i = 0; i < j; i++, j–)
       {
            t = B[i];
            B[i] = B[j];
            B[j] = t;
        }
       printf (“Reversed String is \””);
       for (int k = 0; k < 4; k++)
       {
             printf (“%c”, B[k]);
       }
       printf (“\””);
       return 0;
}

Output:

Reverse a String without extra Array Code in C Language

So, this is the method for reversing a string without using another array.

In the next article, I am going to discuss How to Compare String and Checking Palindrome in C Language with Examples. Here, in this article, I try to explain How to Reverse a String in C Language with Examples. I hope you enjoy this How to Reverse a String in C Language with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.