Bird
Raised Fist0
DSA Cprogramming~10 mins

String Reversal Approaches in DSA C - Execution Trace

Choose your learning style10 modes available
Concept Flow - String Reversal Approaches
Start with original string
↓
Choose reversal method
↓
Method 1: Swap ends
↓
Swap characters from ends
↓
Move pointers inward
↓
Repeat until pointers meet
↓
Reversed string
The flow shows two main ways to reverse a string: swapping characters from the ends moving inward, or building a new string by appending characters from the end to the start.
Execution Sample
DSA C
void reverse_swap(char *s) {
  int i = 0, j = strlen(s) - 1;
  while (i < j) {
    char temp = s[i]; s[i] = s[j]; s[j] = temp;
    i++; j--;
  }
}
This code reverses a string by swapping characters from the start and end moving inward until they meet.
Execution Table
StepOperationijCharacters SwappedString State
1Initialize pointers04None"hello"
2Swap s[0] and s[4]04'h' <-> 'o'"oellh"
3Move pointers inward13None"oellh"
4Swap s[1] and s[3]13'e' <-> 'l'"olleh"
5Move pointers inward22None"olleh"
6Check condition i < j22No swap, pointers met"olleh"
💡 Pointers i and j met at index 2, no more swaps needed, string reversed.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6 (Final)
i0122
j4322
String"hello""oellh""olleh""olleh"
Key Moments - 3 Insights
Why do we stop swapping when i equals j?
Because when i equals j, it means the pointers have met in the middle, so all characters have been swapped. See execution_table step 6 where i=2 and j=2, no swap occurs.
Why do we swap characters at s[i] and s[j]?
Swapping s[i] and s[j] moves the character from the start to the end and vice versa, gradually reversing the string. This is shown in steps 2 and 4 where characters are swapped.
What happens if the string length is odd?
The middle character stays in place because swapping stops when pointers meet. The middle character does not need to move, as shown at step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the string state after step 4?
A"olleh"
B"hello"
C"oellh"
D"hlelo"
💡 Hint
Check the 'String State' column at step 4 in the execution_table.
At which step do the pointers i and j meet?
AStep 2
BStep 6
CStep 4
DStep 5
💡 Hint
Look at the 'i' and 'j' values in the execution_table rows to find when they are equal.
If the string was "abcd", what would be the string state after the first swap?
A"cbda"
B"dcba"
C"dbca"
D"abcd"
💡 Hint
Swapping first and last characters swaps 'a' and 'd'.
Concept Snapshot
String Reversal Approaches:
- Method 1: Swap characters from ends moving inward.
- Method 2: Build a new string from end to start.
- Stop swapping when pointers meet or cross.
- Works for strings of any length.
- In-place reversal saves space.
Full Transcript
This concept shows two main ways to reverse a string. The first method swaps characters from the start and end moving inward until the pointers meet. The second method builds a new string by appending characters from the end to the start. The example code uses the first method, swapping characters at positions i and j, then moving i forward and j backward. The process stops when i meets or passes j. The execution table traces each swap and pointer movement, showing the string changing from 'hello' to 'olleh'. Key moments clarify why swapping stops when pointers meet and how the middle character behaves in odd-length strings. The visual quiz tests understanding of string states and pointer positions during reversal.

Practice

(1/5)
1. What does the string reversal operation do to the string "hello"?
easy
A. It changes it to "olleh"
B. It changes it to "hello" (no change)
C. It removes all vowels
D. It converts all letters to uppercase

Solution

  1. Step 1: Understand string reversal

    Reversing a string means writing its characters in the opposite order.
  2. Step 2: Apply reversal to "hello"

    The characters 'h', 'e', 'l', 'l', 'o' become 'o', 'l', 'l', 'e', 'h'.
  3. Final Answer:

    "olleh" -> Option A
  4. Quick Check:

    Reverse("hello") = "olleh" [OK]
Hint: Reverse means flip characters order [OK]
Common Mistakes:
  • Thinking reversal changes letters, not order
  • Confusing reversal with uppercase conversion
  • Assuming reversal removes characters
2. Which of the following C code snippets correctly reverses a string str in place?
easy
A. 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; }
B. for(int i = 0; i < strlen(str); i++) { str[i] = str[strlen(str)-1-i]; }
C. strrev(str);
D. for(int i = 0; i < strlen(str); i++) { str[i] = toupper(str[i]); }

Solution

  1. Step 1: Understand in-place reversal logic

    Swap characters from start and end moving towards center, stopping at half length.
  2. 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.
  3. Final Answer:

    Option A code correctly reverses string in place -> Option A
  4. Quick Check:

    Swapping pairs from ends reverses string [OK]
Hint: Swap pairs from ends to middle [OK]
Common Mistakes:
  • Overwriting characters without swapping
  • Using full length instead of half length in loop
  • Confusing string reversal with uppercase conversion
3. What is the output of the following C code?
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);
medium
A. abcde
B. Error: infinite loop
C. edcde
D. edcba

Solution

  1. 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.
  2. Step 2: Print the reversed string

    After swaps, str is "edcba" which is printed.
  3. Final Answer:

    edcba -> Option D
  4. Quick Check:

    Swapped pairs reverse string [OK]
Hint: Swapping ends moves characters inward [OK]
Common Mistakes:
  • Not updating i and j correctly
  • Confusing loop condition causing infinite loop
  • Printing original string without reversal
4. Identify the error in this C code that attempts to reverse a string 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;
}
medium
A. Loop condition should be i < len/2 not <=.
B. Missing null terminator assignment after reversal.
C. Index len - i is out of bounds; should be len - i - 1.
D. Using strlen inside loop causes performance issue.

Solution

  1. 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).
  2. Step 2: Correct index to len - i - 1

    Adjusting index fixes out-of-bounds error and correctly swaps characters.
  3. Final Answer:

    Index len - i is out of bounds; should be len - i - 1 -> Option C
  4. Quick Check:

    String last index = len-1, not len [OK]
Hint: String last index = length - 1 [OK]
Common Mistakes:
  • Using length as index directly
  • Loop running one extra iteration
  • Ignoring null terminator position
5. You want to reverse only the vowels in a string "education" using C. Which approach correctly achieves this?
hard
A. Reverse entire string and then remove consonants.
B. Use two pointers from start and end, swap vowels only, skip consonants.
C. Copy vowels to a new string and print it reversed.
D. Replace vowels with uppercase letters in place.

Solution

  1. Step 1: Understand vowel-only reversal

    We must swap vowels at start and end positions, ignoring consonants.
  2. 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.
  3. 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.
  4. Final Answer:

    Two pointers swapping vowels only -> Option B
  5. Quick Check:

    Two-pointer vowel swap reverses vowels [OK]
Hint: Swap vowels with two pointers, skip consonants [OK]
Common Mistakes:
  • Reversing entire string instead of vowels only
  • Removing consonants accidentally
  • Changing vowels without reversing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard