How to Reverse a String
Here, you will learn and get the example code of C++ Program to Reverse a String using strrev() function and without using strrev() function.
Reverse a string is the process of reversing the order of the characters in a string.
For example:-
“Welcome to coderevise.com” will be “moc.esiveredoc ot emocleW”
To quick reverse a string in C++ strrev() function is also available here. Here, we are doing this program with and without using strrev() function.
C++ Program to Reverse a String using strrev() function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<iostream> #include<string.h> using namespace std; int main() { char st[100], rev[100]; int i=0, j, lan = 0; cout<<"Enter a String : "; cin>>st; cout << "String Before Reverse is : " << st; cout << "\nString After Reverse is : " << strrev(st); return 0; } |
Output
C++ Program to Reverse a String without using strrev() function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include<iostream> using namespace std; int main() { char st[100], rev[100]; int i=0, j, lan = 0; cout<<"Enter a String : "; cin>>st; cout << "String Before Reverse is : " << st; //get the length of string while(st[lan] != '\0') { lan++; } j = lan - 1; //String reverse while(j>=0) { rev[i]=st[j]; i++; j--; } rev[i]='\0'; cout << "\nString After Reverse is : " << rev; return 0; } |
Output
Check out our other C++ programming Examples