Updated June 12, 2023
Introduction to Reverse String in C
A reverse string can be defined as an operation in which the original string which the user gives is modified in such a way that the characters in it are arranged in a reverse manner starting from the last character to the first character, thus forming a new string which will be the exact reverse of the original string while in case of a C language, the reverse string algorithm will take input as a string and apply the algorithm to reverse it character by character and then it will return the newly formed reversed string to the user.
We can apply the same logic as mentioned in the definition to reverse a string; we can traverse characters in a string from end to start and append one after one. This way, we will have a new string formed by reverse traversal, and this string will be the reversed string. In C language, as we don’t have support for a string data type, we need to use a character array instead. It is easy here to traverse the character array character by character and form a new character array.
Examples of Reverse String in C
Following are the different examples of reverse string in c using various methods.
Example #1 – Using For Loop
Code:
#include <stdio.h>
#include <string.h>
int main ()
{
// char array to take input
char inputString[100];
// char array to build output
char outputString[100];
int length;
int i;
// Take input from the user : input in character array
printf( "Please Enter a string to be reversed \n" );
scanf( "%s", inputString );
// Find the number of characters or length of a string using in built function strlen() from string.h library
length = strlen( inputString );
int j = 0;
// Traverse character by character from end to start and form a new string
for( i = length - 1; i >= 0; i--) {
outputString[ j ] = inputString[ i ];
j++;
}
printf( "The reversed string is: ");
printf( "%s", outputString );
printf( "\n" );
return 0;
}
Output:
Here, we have used strlen() from the < string.h> library to find out the count of characters present in the input string and pass it in for loop. We have parsed the array from end to start and appended characters in reverse order in an output array using for loop.
Example #2 – Using While Loop
Code:
#include <stdio.h>
#include <string.h>
int main ()
{
// char array to take input
char inputString[100];
// char array to build output
char outputString[100];
int length;
int i;
// Take input from the user : input in character array
printf( "Please Enter a string to be reversed \n" );
scanf( "%s", inputString );
// Find the number of characters or length of a string using in built function strlen() from string.h library
length = strlen( inputString );
int j = 0;
// Traverse character by character from end to start and form a new string
i = length - 1;
while( i >= 0) {
outputString[ j ] = inputString[ i ];
i--;
j++;
}
printf( "The reversed string is: ");
printf( "%s", outputString );
printf( "\n" );
return 0;
}
Output:
Example #3 – Using Do While Loop
Let’s modify the same code with a do-while loop.
Code:
#include <stdio.h>
#include <string.h>
int main ()
{
// char array to take input
char inputString[100];
// char array to build output
char outputString[100];
int length;
int i;
// Take input from the user : input in character array
printf( "Please Enter a string to be reversed \n" );
scanf( "%s", inputString );
// Find the number of characters or length of a string using in built function strlen() from string.h library
length = strlen( inputString );
int j = 0;
// Traverse character by character from end to start and form a new string
i = length - 1;
do {
outputString[ j ] = inputString[ i ];
i--;
j++;
}while( i >= 0);
printf( "The reversed string is: ");
printf( "%s", outputString );
printf( "\n" );
return 0;
}
Output:
We can’t enter an empty string in the input because the C language will not allow it.
Example #4 – Using Swapping
Code:
#include <stdio.h>
#include <string.h>
int main ()
{
// char array to take input
char inputString[100];
int length;
int i;
// Take input from the user : input in character array
printf( "Please Enter a string to be reversed \n" );
scanf( "%s", inputString );
// Find the number of characters or length of a string using in built function strlen() from string.h library
length = strlen( inputString );
// swap characters from start with characters from end
int j = length -1;
char temp;
for( i = 0; i <= (length-1) /2; i++) {
temp = inputString[i];
inputString[i] = inputString[j];
inputString[j] = temp;
j--;
}
printf( "The reversed string is: ");
printf( "%s", inputString );
printf( "\n" );
return 0;
}
Output:
Here, we are not using any extra character array for storage. We are modifying the existing input character array by swapping the characters from the start with the characters from the end. In this case, we need to use only one extra memory space for storage.
Conclusion
String reverse is an operation in which the sequence of characters in a string is reversed. C language provides efficient ways to implement and perform this operation. In this article, we have seen various methods by which we can perform the reverse operation.
Recommended Articles
This is a guide to Reverse String in C. Here we discuss the Introduction along with different examples and code implementation. You may also look at the following articles to learn more –