Challenge - 5 Problems
String Operations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Remember that strlen counts characters before the null terminator.
✗ Incorrect
The string "Hello, C!" has 9 characters including the comma and space but excluding the null terminator.
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
strcat appends the second string to the first one.
✗ Incorrect
strcat adds the contents of s2 to the end of s1, resulting in "CodeMaster".
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
strcmp returns a negative value if the first string is lexicographically less.
✗ Incorrect
"apple" is lexicographically less than "apricot", so strcmp returns a negative number and prints "A".
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
strcpy does not support overlapping source and destination.
✗ Incorrect
Using strcpy with overlapping memory causes undefined behavior, so output may be corrupted or unpredictable.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Use the buffer size, not the source length, to limit snprintf.
✗ Incorrect
Option A uses the buffer size to limit snprintf, preventing overflow and ensuring null termination.