0
0
Cprogramming~20 mins

Common string operations - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Operations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of string length calculation
What is the output of this C code snippet?
C
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello, C!";
    printf("%zu", strlen(str));
    return 0;
}
A8
B11
C10
D9
Attempts:
2 left
💡 Hint
Remember that strlen counts characters before the null terminator.
Predict Output
intermediate
2:00remaining
Result of string concatenation with strcat
What will be printed by this C program?
C
#include <stdio.h>
#include <string.h>

int main() {
    char s1[20] = "Code";
    char s2[] = "Master";
    strcat(s1, s2);
    printf("%s", s1);
    return 0;
}
ACode Master
BMasterCode
CCode
DCodeMaster
Attempts:
2 left
💡 Hint
strcat appends the second string to the first one.
Predict Output
advanced
2:00remaining
Output of string comparison
What is the output of this C code?
C
#include <stdio.h>
#include <string.h>

int main() {
    char a[] = "apple";
    char b[] = "apricot";
    int result = strcmp(a, b);
    if (result < 0) {
        printf("A");
    } else if (result > 0) {
        printf("B");
    } else {
        printf("C");
    }
    return 0;
}
AA
BB
CCompilation error
DC
Attempts:
2 left
💡 Hint
strcmp returns a negative value if the first string is lexicographically less.
Predict Output
advanced
2:00remaining
Behavior of strcpy with overlapping strings
What happens when this C code runs?
C
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "OverlapTest";
    strcpy(str + 3, str);
    printf("%s", str);
    return 0;
}
AOverlapTestOverlapTest
BUndefined behavior (may print corrupted string)
COverTest
DCompilation error
Attempts:
2 left
💡 Hint
strcpy does not support overlapping source and destination.
🧠 Conceptual
expert
2:00remaining
Correct use of snprintf to avoid buffer overflow
Which option correctly uses snprintf to safely copy a string into a fixed-size buffer without overflow?
Asnprintf(buffer, sizeof(buffer), "%s", source);
Bsnprintf(buffer, strlen(source), "%s", source);
Csnprintf(buffer, sizeof(source), "%s", source);
Dsnprintf(buffer, sizeof(buffer) - 1, "%s", source);
Attempts:
2 left
💡 Hint
Use the buffer size, not the source length, to limit snprintf.