String Reversal Approaches in DSA C - Time & Space Complexity
We want to understand how the time needed to reverse a string changes as the string gets longer.
How does the number of steps grow when the string size grows?
Analyze the time complexity of the following code snippet.
void reverseString(char* str) {
int start = 0;
int end = strlen(str) - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
This code reverses a string in place by swapping characters from the start and end moving towards the center.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Swapping two characters inside a loop.
- How many times: About half the length of the string (n/2 times).
As the string gets longer, the number of swaps grows roughly in direct proportion to its length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 swaps |
| 100 | 50 swaps |
| 1000 | 500 swaps |
Pattern observation: Doubling the string length roughly doubles the number of swaps.
Time Complexity: O(n)
This means the time to reverse the string grows linearly with the string length.
[X] Wrong: "Reversing a string takes constant time because we just swap characters."
[OK] Correct: Each character must be visited and swapped, so the time grows with the string size, not fixed.
Understanding how simple loops scale helps you explain your code clearly and shows you know how to reason about efficiency.
"What if we used recursion to reverse the string? How would the time complexity change?"
Practice
"hello"?Solution
Step 1: Understand string reversal
Reversing a string means writing its characters in the opposite order.Step 2: Apply reversal to "hello"
The characters 'h', 'e', 'l', 'l', 'o' become 'o', 'l', 'l', 'e', 'h'.Final Answer:
"olleh" -> Option AQuick Check:
Reverse("hello") = "olleh" [OK]
- Thinking reversal changes letters, not order
- Confusing reversal with uppercase conversion
- Assuming reversal removes characters
str in place?Solution
Step 1: Understand in-place reversal logic
Swap characters from start and end moving towards center, stopping at half length.Step 2: Analyze for(int i = 0; i < strlen(str)/2; i++) { char temp = str[i]; str[i] = str[strlen(str)-1-i]; str[strlen(str)-1-i] = temp; }
It swaps str[i] with str[length-1-i] for i from 0 to half length, correctly reversing the string.Final Answer:
Option A code correctly reverses string in place -> Option AQuick Check:
Swapping pairs from ends reverses string [OK]
- Overwriting characters without swapping
- Using full length instead of half length in loop
- Confusing string reversal with uppercase conversion
char str[] = "abcde";
int i = 0, j = 4;
while(i < j) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("%s", str);Solution
Step 1: Trace the while loop swaps
Swap str[0] and str[4]: 'a' and 'e' -> str = "ebcda"; i=1, j=3 Swap str[1] and str[3]: 'b' and 'd' -> str = "edcba"; i=2, j=2 Loop ends as i == j.Step 2: Print the reversed string
After swaps, str is "edcba" which is printed.Final Answer:
edcba -> Option DQuick Check:
Swapped pairs reverse string [OK]
- Not updating i and j correctly
- Confusing loop condition causing infinite loop
- Printing original string without reversal
str:
int len = strlen(str);
for(int i = 0; i <= len/2; i++) {
char temp = str[i];
str[i] = str[len - i];
str[len - i] = temp;
}Solution
Step 1: Check index usage in swapping
String indices go from 0 to len-1. Using str[len - i] accesses one past last character (out of bounds).Step 2: Correct index to
Adjusting index fixes out-of-bounds error and correctly swaps characters.len - i - 1Final Answer:
Indexlen - iis out of bounds; should belen - i - 1-> Option CQuick Check:
String last index = len-1, not len [OK]
- Using length as index directly
- Loop running one extra iteration
- Ignoring null terminator position
"education" using C. Which approach correctly achieves this?Solution
Step 1: Understand vowel-only reversal
We must swap vowels at start and end positions, ignoring consonants.Step 2: Use two pointers approach
Initialize pointers at string start and end, move inward skipping consonants, swap vowels when both pointers point to vowels.Step 3: Confirm other options are incorrect
Reverse entire string and then remove consonants. reverses whole string, not vowels only. Copy vowels to a new string and print it reversed. prints vowels only, losing consonants. Replace vowels with uppercase letters in place. changes vowels but does not reverse.Final Answer:
Two pointers swapping vowels only -> Option BQuick Check:
Two-pointer vowel swap reverses vowels [OK]
- Reversing entire string instead of vowels only
- Removing consonants accidentally
- Changing vowels without reversing
Ready to prove it?
Pass the test and this pattern moves unknown
