
Problem
Solution
Submissions
Reverse a String
Certification: Basic Level
Accuracy: 56.6%
Submissions: 53
Points: 10
Write a C++ program that reverses a string using pointers.
Example 1
- Input: string = "hello"
- Output: "olleh"
- Explanation:
- Step 1: Initialize pointers to the start and end of the string.
- Step 2: Swap characters at start and end positions.
- Step 3: Move start pointer forward and end pointer backward.
- Step 4: Repeat steps 2-3 until pointers meet in the middle.
Example 2
- Input: string = "world"
- Output: "dlrow"
- Explanation:
- Step 1: Initialize pointers to the start and end of the string.
- Step 2: Swap characters at start and end positions.
- Step 3: Move start pointer forward and end pointer backward.
- Step 4: Repeat steps 2-3 until pointers meet in the middle.
Constraints
- 1 ≤ string length ≤ 10^6
- Use pointers to reverse the string
- Time Complexity: O(n) where n is the length of the string
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use two pointers, one at the beginning and one at the end of the string.
- Swap the characters at these pointers and move them towards the center.
- Stop when the pointers meet or cross each other.