String Reversal – CSKC
Sign In

Search

Code Learning #CPP#7632    siteicon   siteicon  

String Reversal

Source Code

TC++ #7632

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Code Understanding

This program takes a string from the user and reverses it using a simple swapping method.

• string str; → declares a C++ style string variable to store user input.
• cout << “Input String to Be Reversed : “; → asks the user to enter a string.
• getline(cin, str); → reads the full line (including spaces) into str.
• int len = str.length(); → finds and stores the length of the string in len.
• char temp; → a temporary variable used for swapping characters.
• for(int i = 0; i < len/2; i++) → runs a loop till the middle of the string.
• temp = str[i]; → stores the character from the beginning.
• str[i] = str[len – i – 1]; → replaces it with the character from the end.
• str[len – i – 1] = temp; → puts the stored character at the end position.
• After the loop, all characters are swapped, and the string becomes reversed.
• cout << str << endl; → displays the reversed string.


Share

Created: 1-Mar-2026 17:44 | Updated: 1-Mar-2026 17:46|