Table of Contents
ToggleWhat is a String in C?
In C programming language, a string is a series of characters stored in contiguous memory locations. Unlike other programming languages, C represents a string as arrays of characters. Strings in C is terminated by a null character represented as “\0”, signifying the end of the string. In this post we will discuss various ways to reverse a string in c.
Code to declare and define a string in C:
Char str[] = “Sample String”;
Meaning of Reversing a String
Reversing a string is an important technique in C programming language. It is a technique that involves inversion of the given string i.e. change in the position of characters such tha the first character becomes the last character, second character becomes the second last, third becomes the third last and so on.
For example, if the inputted string is “TeachingBee” then its reversed string is “eeBgnihcaeT”.
Methods to Reverse a String in C
There are multiple ways to write the program to reverse a string in C programming language:
- Reverse a string using the strrev() function
- Reverse a string without using the library function
- Reverse a string using for loop
- Reverse a string using while loop
- Reverse a string using pointers
- Reverse a string using the recursion function
Reverse a String Using the strrev() Function
Algorithm
- Declare a character array ‘str’ to store the string.
- Ask the user to enter the string.
- Reverse the string using strrev() function.
- Display the reversed string as output.
Source Code
#include <stdio.h>
#include <string.h>
int main() {
char str[40];
printf("\nEnter a string to be reversed: ");
scanf("%s", str);
strrev(str);
printf("\nAfter the reverse of a string: %s", str);
return 0;
}
Output
Reverse a String Without Using the Library Function
Algorithm
- Define a function reverseString().
- In reverseString() function, use a for loop with two indices (‘start’ and ‘end’) to swap characters from the beginning to the end of the string until ‘start’ is less than ‘end’.
- Declare a character array ‘mystring’ to store the string.
- Ask the user to enter the string.
- Call the function named reverseString().
- Display the reversed string as output.
Source Code
#include <stdio.h>
void reverseString(char str[]) {
int length = 0;
while (str[length] != '\0') {
length++;
}
for (int start = 0, end = length - 1; start < end; start++, end--) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
}
}
int main() {
char myString[40];
printf("Enter a string to be reversed: ");
scanf("%s", myString);
reverseString(myString);
printf("After the reverse of a string: %s\n", myString);
return 0;
}
Output
Reverse a String Using for loop
Algorithm
- Declare a character array ‘mystring’ to store the string.
- Ask the user to enter the string.
- Calculate the length of the string.
- Use a for loop that will iterate half the length of input string to swap characters from the beginning with the characters at ‘length-1-i’ position.
- Display the reversed string as output.
Source Code
#include <stdio.h>
#include <string.h>
int main() {
char myString[100];
printf("Enter a string: ");
scanf("%s", myString);
int length = strlen(myString);
if (myString[length - 1] == '\n') {
myString[length - 1] = '\0';
}
for (int i = 0; i < length / 2; i++) {
char temp = myString[i];
myString[i] = myString[length - 1 - i];
myString[length - 1 - i] = temp;
}
printf("Reversed string: %s\n", myString);
return 0;
}
Output
Reverse a String Using while loop
Algorithm
- Declare a character array ‘mystring’ to store the string.
- Ask the user to enter the string.
- Calculate the length of the string.
- Use a while loop that will iterate half the length of input string to swap characters from the beginning with the characters at ‘length-1-i’ position.
- Display the reversed string as output.
Source Code
#include <stdio.h>
#include <string.h>
int main() {
char myString[100];
printf("Enter a string: ");
scanf("%s", myString);
int length = strlen(myString);
if (myString[length - 1] == '\n') {
myString[length - 1] = '\0';
length--;
}
int i = 0;
while (i < length / 2) {
char temp = myString[i];
myString[i] = myString[length - 1 - i];
myString[length - 1 - i] = temp;
i++;
}
printf("Reversed string: %s\n", myString);
return 0;
}
Output
Reverse a String Using Pointers
Algorithm
- Define a constant ‘MAX_LENGTH’ for the maximum length of the input string.
- Define a function reverseString().
- In reverseString() function, create two pointers ‘start’ and ‘end’ and then use a while loop that will iterate half the length of input string to swap characters from the beginning to the end of the string.
- In main() function, declare a character array ‘mystring’ to store the string.
- Ask the user to enter the string.
- Call the function named reverseString().
- Display the reversed string as output.
Source Code
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 100
void reverseString(char *str) {
int length = strlen(str);
char *start = str;
char *end = str + length - 1;
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main() {
char myString[MAX_LENGTH];
printf("Enter a string: ");
scanf("%s", myString);
reverseString(myString);
printf("Reversed string: %s\n", myString);
return 0;
}
Output
Reverse a String using Recursion
Algorithm
- Define a function reverseString().
- In reverseString() function, if ‘start’ is less than ‘end’, swap the characters at indices ‘start’ and ‘end’. And call the reverseString() function recursively until the condition i.e. (start<end) satisfies.
- In main() function, declare a character array ‘mystring’ to store the string.
- Ask the user to enter the string.
- Call the function named reverseString().
- Display the reversed string as output.
Source Code
#include <stdio.h>
#include <string.h>
void reverseString(char str[], int start, int end) {
if (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
reverseString(str, start + 1, end - 1);
}
}
int main() {
char myString[100];
printf("Enter a string: ");
scanf("%s", myString);
int length = strlen(myString);
reverseString(myString, 0, length - 1);
printf("Reversed string: %s\n", myString);
return 0;
}
Output
Conclusion
In conclusion, string reversal in C is an important skill for every programmer. This blog has highlighted multiple methods, from traditional loops to modern concepts. Mastering this technique not only helps in problem-solving but also builds foundation for solving complex programming challenges. The knowledge gained in the blog is a stepping stone toward becoming a more versatile programmer. Happy learning!
Checkout more C Tutorials here.
I hope You liked the post 👍. For more such posts, 📫 subscribe to our newsletter. Try out our free resume checker service where our Industry Experts will help you by providing resume score based on the key criteria that recruiters and hiring managers are looking for.
FAQ
How do you reverse a string in C?
To reverse a string in C, you can use the strrev()
function provided in some C standard libraries. or there are other methods like loops, two pointers, recursion.
How do you reverse a string in C++?
In C++, you can reverse a string by using the std::reverse
function from the <algorithm>
header, passing the beginning and end of the string, or by manually swapping characters in a loop.
How to reverse a string?
To reverse a string, iterate over half of its length and swap each character with its corresponding character from the other end of the string. This can be done in most programming languages using a simple loop.
How to reverse a string using swap in C?
Reverse a string using swap in C by creating two pointers ‘start’ and ‘end’ and then use a while loop that will iterate half the length of input string to swap characters from the beginning to the end of the string.