Bird
0
0
DSA Cprogramming~10 mins

String Reversal Approaches in DSA C - Execution Trace

Choose your learning style9 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.