Challenge - 5 Problems
String Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of strcmp with equal strings
What is the output of the following C code?
C
#include <stdio.h> #include <string.h> int main() { char a[] = "hello"; char b[] = "hello"; int result = strcmp(a, b); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
strcmp returns 0 when strings are equal.
✗ Incorrect
The strcmp function returns 0 when both strings are exactly the same.
❓ Predict Output
intermediate2:00remaining
Output of strcmp with first string less than second
What is the output of this C code snippet?
C
#include <stdio.h> #include <string.h> int main() { char a[] = "apple"; char b[] = "banana"; int result = strcmp(a, b); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
strcmp returns negative if first string is less in lexicographic order.
✗ Incorrect
Since "apple" is lexicographically less than "banana", strcmp returns a negative value.
❓ Predict Output
advanced2:00remaining
Output of strcmp with case difference
What will this C program print?
C
#include <stdio.h> #include <string.h> int main() { char a[] = "Hello"; char b[] = "hello"; int result = strcmp(a, b); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
Uppercase letters have lower ASCII codes than lowercase letters.
✗ Incorrect
Since 'H' (ASCII 72) is less than 'h' (ASCII 104), strcmp returns a negative number.
❓ Predict Output
advanced2:00remaining
Output of strcmp with empty string
What is the output of this C code?
C
#include <stdio.h> #include <string.h> int main() { char a[] = ""; char b[] = "nonempty"; int result = strcmp(a, b); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
Empty string is less than any non-empty string.
✗ Incorrect
strcmp returns a negative number because empty string is lexicographically less than "nonempty".
❓ Predict Output
expert2:00remaining
Value of variable after strcmp with partial match
What is the value of variable result after running this code?
C
#include <stdio.h> #include <string.h> int main() { char a[] = "test123"; char b[] = "test124"; int result = strcmp(a, b); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
strcmp compares character by character until difference found.
✗ Incorrect
The strings differ at the last character: '3' (ASCII 51) vs '4' (ASCII 52), so strcmp returns a negative number (-1).