Reverse a String
C++
Easy
4 views
Problem Description
Reverse the order of characters in string.
Real Life: Like reading text backwards.
Step-by-Step Logic:
1. Use two pointers: start and end
2. Swap characters at start and end
3. Move start forward, end backward
4. Continue until they meet
Official Solution
void string_q4_reverse() {
string text = "Programming";
int start = 0;
int end = text.length() - 1;
cout << "Original: " << text << endl;
while(start < end) {
char temp = text[start];
text[start] = text[end];
text[end] = temp;
start++;
end--;
}
cout << "Reversed: " << text << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!