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.
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:
So once we are on the last alphabet, we can start copying those elements in another array in reverse order as:
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’.
So ‘j’ is pointing to the last element. We have to copy that element in another array:
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’.
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:
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.
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:
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.