Tutorial Playlist
132 Lessons1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
Now Reading
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
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
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
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...