Bird
Raised Fist0
DSA Cprogramming~10 mins

String Reversal Approaches in DSA C - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the reversed string using a loop.

DSA C
char str[] = "hello";
int len = 5;
for (int i = len - 1; i >= [1]; i--) {
    printf("%c", str[i]);
}
printf("\n");
Drag options to blanks, or click blank then click option'
A0
B1
C-1
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 as the loop's stopping condition.
Starting the loop from len instead of len - 1.
2fill in blank
medium

Complete the code to swap characters for in-place string reversal.

DSA C
char str[] = "world";
int len = 5;
for (int i = 0; i < [1]; i++) {
    char temp = str[i];
    str[i] = str[len - i - 1];
    str[len - i - 1] = temp;
}
printf("%s\n", str);
Drag options to blanks, or click blank then click option'
Alen - 1
Blen
Clen + 1
Dlen / 2
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping characters for the entire length causing double swaps.
Using len instead of len / 2 as the loop limit.
3fill in blank
hard

Fix the error in the recursive string reversal function call.

DSA C
void reverse(char *str, int start, int end) {
    if (start >= end) return;
    char temp = str[start];
    str[start] = str[end];
    str[end] = temp;
    reverse(str, [1], end - 1);
}
Drag options to blanks, or click blank then click option'
Aend - 1
Bstart + 1
Cend + 1
Dstart - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Decreasing start instead of increasing it.
Using the same indices causing infinite recursion.
4fill in blank
hard

Fill both blanks to create a loop that copies characters from the end to the start in a new string.

DSA C
char str[] = "abcde";
char rev[6];
int len = 5;
for (int i = 0; i < [1]; i++) {
    rev[i] = str[[2] - i - 1];
}
rev[len] = '\0';
printf("%s\n", rev);
Drag options to blanks, or click blank then click option'
Alen
Blen - 1
Clen + 1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using len - 1 as loop limit causing missing last character.
Incorrect index calculation causing wrong characters copied.
5fill in blank
hard

Fill all three blanks to create a function that returns a new reversed string.

DSA C
char* reverse_string(const char* str) {
    int len = 0;
    while (str[len] != '\0') {
        len++;
    }
    char* rev = malloc(sizeof(char) * (len + 1));
    for (int i = 0; i < [1]; i++) {
        rev[i] = str[[2] - i - 1];
    }
    rev[[3]] = '\0';
    return rev;
}
Drag options to blanks, or click blank then click option'
Alen
Blen - 1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Not allocating enough memory for the null terminator.
Incorrect loop limits causing incomplete 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