Tutorial Playlist
The string reverse approach suggests the change in the sequence of characters in a given string in a way that the string’s last character becomes the first character, and so on, performing the swapping of characters. Let’s dive into the following tutorial and explore various ways to perform string reversal in C!
You may encounter a situation where you have to check whether an input string is palindrome or not. A palindrome string stays the same when reversed. Various ways are available if you want to reverse a string in C to check for palindrome or encrypt a text, or other purposes.
When the reverse of a string algorithm is implemented in a particular input string, the string entered in a certain order by the user is entirely reversed. For example, if the input string is “world”, the output string will be “dlrow”, implying the string is not palindrome.
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
The strrev() is a built-in function defined in the <string.h> header file. Its syntax is:
char *strrev(char *str) |
In the above syntax, str is the string that needs to be reversed. This function takes a single parameter and returns the revised version of the reversed string.
Here’s an example program in C to understand how strrev() function works.
#include<stdio.h> |
Output:
The input string is Reading |
The above program calls the strrev( ) on the given string, and the output displays the reversed string.
Note: Since the strrev function doesn’t belong to the C standard library, you might get an error for the above program if it’s not available on your compiler.
If you want to reverse a string in C without strrev, you can execute the following program.
#include <stdio.h> |
Output:
Please enter a string: Dance |
As seen from the above program, the reverseString function reverses the entered string str. It does this by swapping characters from the start and end of the string using a two-pointer approach. The user is prompted to enter the input string in the main function. Subsequently, the main function calls the reverseString function and prints the reversed string.
If you want to reverse a string in C using function, you must first determine the string’s length. Subsequently, you need to run a for loop to allocate characters of another string equal to the characters of the entered string in reverse order. The part of this program that uses for loop will resemble the program that reverse a string in C using for loop.
#include <stdio.h> |
Output:
The given string is education |
First, we calculate the length of the entered string and initialize an empty string. Subsequently, the program assigns all the character values of the entered string to that of the new string in the reverse sequence. The iteration continues till the determined length of the string.
Using the recursion function to reverse a string in C makes the code more efficient. This function substitutes the basic iteration, but the swapping technique stays the same. The following program recursively calls the function containing logic. Moreover, each character is swapped.
#include <stdio.h> |
Output:
Please enter a string: English |
The reverseString function in the above program recursively checks if the current character (*str) is not the null terminator (i.e. \0') If it’s not null, it calls itself with the subsequent character (str + 1) and prints the current character. The recursive function processes the string in reverse order and displays the reversed string at the output.
The main function requests the user to enter a string through fgets. Subsequently, it calls the reverseString function to reverse the string.
This method involves reversing the characters’ orders in an input string using a for loop in C. Here’s an example program.
#include <stdio.h> |
Output:
Please enter a string: College |
In this code, we have used a function called reverseyourString that accepts a character array (string) as input and then reverses it using a for loop. This loop begins from both ends of the string and swaps each character at corresponding positions until the central one is reached. Lastly, the program prints the reversed string in the main function.
Here’s an example program that uses a while loop to reverse an input string.
#include <stdio.h> |
Output:
Please enter a string: Study |
In this program, the reverseyourString function accepts a character array as input. It initializes a first variable to 0 (the string’s beginning) and a last variable to length - 1 (the string’s end).
In the while loop, the defined function swaps the characters at first and last using a tem variable. Subsequently, it increments first and decrements last to move nearer to the center of the string. The while loop continues until the first becomes greater than or equal to the last.
The user is prompted to enter a string in the main function. This is then passed to reverseyourstring function. Lastly, the printf function prints the reversed string as the output.
The reverseString function in the below program reverses the input string strng using two pointers: begin and end. The begin pointer first points to the string’s first character, whereas the end pointer is moved to the last character of the string. This is done by repeating until it reaches the null terminator ('\0'). The program performs character-by-character swapping.
Next, the function uses two pointers to reverse the string by swapping each character. The begin pointer is incremented, and the end pointer is decremented until they run into the center of the string.
The main function allows the user to enter a string through fgets function. Subsequently, it calls the reverseString function to perform string reversal.
#include <stdio.h> |
Output:
Please enter a string: learn |
Here’s the program:
#include <stdio.h> |
Output:
Please enter a string: Education |
In the above code, reverseaString function accepts two parameters: strg and reversed. We’ve used a for loop to iterate on the input string’s characters in reverse order. In the main function, the program prompts the user to enter a string. It is subsequently passed with the reversed string to the reverseaString function. Finally, the reversed array stores the reversed string and prints it at the output.
Here’s an example program to reverse an input string by displaying it in reverse order.
#include <stdio.h> |
Output:
Please enter a string: Technology |
In the above program, the main function lets the user input a string through the fgets function. It subsequently iterates over the characters of the string in reverse sequence. Specifically, it begins with the last character (strlen(str) - 1) and ends with the first character (0).
The string reversal capability in C helps in encryption, palindrome, etc. The most common method to reverse your string in C is the swapping technique. Moreover, string reversal allows you to search and sort by the end of strings.
Tutorials are certainly one of the best approaches to strengthening your fundamentals in C. In addition to going through the tutorial, mastering the latest technical skills is also a vital facet that aspirants must consider, and this is what upGrad assists with!
Along with getting familiar with the string reversal methods in C, check out upGrad’s Master of Science in Computer Science Program by Liverpool John Moores University to significantly enhance your career graph in the tech industry!
1. Can you reverse a string without altering the original string?
You can reverse a string in C without altering the original string by defining a new string and copying the characters in the reverse order.
2. Is it possible to reverse a string in C using library functions?
String reversal in C is possible using library functions. The strrev function from the <string.h> library is useful for the same. But you must take into account that the strrev function doesn’t belong to the C standard library, and it might not be available on all compilers or systems.
3. What is the time complexity and space complexity of reversing a string in C? Explain with reasons for each.
The time complexity of reversing a string in C is O(n) (where n is the string’s length). The reason is you have to iterate over each character of the string once to accomplish reversal. The space complexity of reversing a string in C is O(1). The reason is the reversal is performed in-place, and additional memory is not required. The additional memory is required only when using a separate string to save the reversed output.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...