Bird
0
0
DSA Cprogramming~10 mins

String Reversal Approaches in DSA C - Interactive Practice

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