Challenge - 5 Problems
String Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of string concatenation with
strcatWhat is the output of this C program?
C
#include <stdio.h> #include <string.h> int main() { char str1[20] = "Hello"; char str2[] = " World"; strcat(str1, str2); printf("%s", str1); return 0; }
Attempts:
2 left
💡 Hint
Remember that strcat appends the second string to the first one.
✗ Incorrect
The function strcat appends str2 to the end of str1. Since str1 has enough space, the result is "Hello World".
❓ Predict Output
intermediate2:00remaining
Result of
strlen on a stringWhat is the output of this C program?
C
#include <stdio.h> #include <string.h> int main() { char str[] = "C programming"; printf("%zu", strlen(str)); return 0; }
Attempts:
2 left
💡 Hint
strlen counts characters before the null terminator.
✗ Incorrect
The string "C programming" has 13 characters including the space. strlen returns 13.
❓ Predict Output
advanced2:00remaining
Output of
strcmp comparing two stringsWhat is the output of this C program?
C
#include <stdio.h> #include <string.h> int main() { char s1[] = "apple"; char s2[] = "apples"; int result = strcmp(s1, s2); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
strcmp returns negative if first string is less than second.
✗ Incorrect
Since "apple" is shorter and lexicographically less than "apples", strcmp returns a negative value.
❓ Predict Output
advanced2:00remaining
Behavior of
strcpy with overlapping stringsWhat is the output of this C program?
C
#include <stdio.h> #include <string.h> int main() { char str[] = "overlap"; strcpy(str + 2, str); printf("%s", str); return 0; }
Attempts:
2 left
💡 Hint
strcpy does not support overlapping source and destination.
✗ Incorrect
Using strcpy with overlapping source and destination causes undefined behavior. The output may vary or crash.
🧠 Conceptual
expert2:00remaining
Which function safely copies strings with buffer size limit?
Which of the following C functions is designed to copy strings safely by limiting the number of characters copied to avoid buffer overflow?
Attempts:
2 left
💡 Hint
One function lets you specify the maximum number of characters to copy.
✗ Incorrect
strncpy copies up to a specified number of characters, helping prevent buffer overflow. strcpy does not limit copying.