
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reverse a String in C/C++
There can be multiple ways to reverse a string in C and C++. In this article, we will learn reversing a string using different approaches with the appropriate examples.
Consider the below example with input and output scenarios:
Input // characters representation from left to right inp = "Tutorialspoint" Output // characters representation from right to left out = tniopslairotuT
Now, we have following approaches to solve the reverse of a given approach:
Reverse String Using Two Pointer
Two-pointer is one of the best technique to reverse the given string. It initializes the pointer at the left in the start and right in the end. Then use logic of swapping and assign increment and decrement operator to solve the process of reverse characters.
Example
In this example, we use two-pointer technique that result the string characters from right to left:
#include <stdio.h> #include <string.h> // function to reverse a string in place void revString(char *str) { // Pointer intialization for left and right side characters int left = 0, right = strlen(str) - 1; // loop until left pointer crosses right pointer while (left < right) { char temp = str[left]; str[left] = str[right]; str[right] = temp; left++; right--; } } int main() { char str[] = "Tutorialspoint"; revString(str); printf("Reversed String: %s\n", str); return 0; }
Output
The above program produces the following result:
Reversed String: tniopslairotuT
#include <iostream> #include <cstring> using namespace std; // function to reverse a string in place void revString(char *str) { int left = 0, right = strlen(str) - 1; while (left < right) { char temp = str[left]; str[left] = str[right]; str[right] = temp; left++; right--; } } int main() { char str[] = "Tutorialspoint"; revString(str); cout << "Reversed String: " << str << endl; return 0; }
Output
The above program produces the following result:
Reversed String: tniopslairotuT
Reverse String Using reverse() Function
You can also use the reverse() function, it is an in-built of the
Demonstration of begin() and end() in the characters form.
Initial string : "Hello" Swap the characters: 'H' > 'o' > "oellH" 'e' > 'l' > "olleH" ...
Example
Following is the basic example of reverse() function to solve the reverse of a string:
#include<iostream> #include<algorithm> #include<string> using namespace std; int main() { string str = "Tutorix"; reverse(str.begin(), str.end()); // display the result cout << "Reversed String: " << str << endl; return 0; }
Output
The above program produces the following result:
Reversed String: xirotuT
Reverse String Using strlen() Function
Another simple approach is to iterate over the characters of the string using the stelen() function and loop. In this approach, we simply get the index of the last character using the strlen(str) - 1 and then run the loop from this index to 0.
Example
In this example, use the strlen() function to show the usage of reverse string:
#include <iostream> #include <cstring> using namespace std; int main() { char str[] = "samsung"; cout << "Original string: " << str; cout << endl << "String after reverse: "; for (int i = strlen(str) - 1; i >= 0; i--) { // diplay the reverse characters cout << str[i]; } return 0; }
Output
The above program produces the following result:
gnusmas