Bird
0
0
DSA Cprogramming~20 mins

String Reversal Approaches in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Reversal Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of string reversal using two-pointer swap
What is the output of the following C code that reverses a string using two pointers swapping characters?
DSA C
void reverse(char *str) {
    int left = 0;
    int right = strlen(str) - 1;
    while (left < right) {
        char temp = str[left];
        str[left] = str[right];
        str[right] = temp;
        left++;
        right--;
    }
}

int main() {
    char s[] = "hello";
    reverse(s);
    printf("%s", s);
    return 0;
}
Aolleh
Bhello
Cohlle
DSyntax error
Attempts:
2 left
💡 Hint
Think about swapping characters from start and end moving towards the center.
Predict Output
intermediate
2:00remaining
Output of string reversal using recursion
What is the output of this C code that prints a string in reverse using recursion?
DSA C
#include <stdio.h>
void reversePrint(char *str) {
    if (*str == '\0') return;
    reversePrint(str + 1);
    putchar(*str);
}

int main() {
    char s[] = "abc";
    reversePrint(s);
    return 0;
}
Aabc
Bcba
CSyntax error
DRuntime error
Attempts:
2 left
💡 Hint
The function calls itself with the next character before printing the current one.
🧠 Conceptual
advanced
2:00remaining
Why is in-place reversal more memory efficient?
Which reason best explains why reversing a string in-place (modifying the original string) is more memory efficient than creating a new reversed string?
AIt uses a linked list internally
BIt compresses the string during reversal
CIt avoids allocating extra memory for a new string copy
DIt uses recursion which saves memory
Attempts:
2 left
💡 Hint
Think about memory allocation for new data versus modifying existing data.
🔧 Debug
advanced
2:00remaining
Identify the error in this string reversal code
What error does this C code produce when trying to reverse a string literal?
DSA C
void reverse(char *str) {
    int left = 0;
    int right = strlen(str) - 1;
    while (left < right) {
        char temp = str[left];
        str[left] = str[right];
        str[right] = temp;
        left++;
        right--;
    }
}

int main() {
    char *s = "hello";
    reverse(s);
    printf("%s", s);
    return 0;
}
ASegmentation fault (runtime error)
BCompilation error
COutput: olleh
DNo output
Attempts:
2 left
💡 Hint
String literals are stored in read-only memory in C.
🚀 Application
expert
2:00remaining
Result of reversing a palindrome string in-place
Given the string "madam", what is the state of the string after applying an in-place reversal function?
ASyntax error
Bmadma
Cmadam\0
Dmadam
Attempts:
2 left
💡 Hint
A palindrome reads the same forwards and backwards.