0
0
Cprogramming~20 mins

String comparison - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
AUndefined behavior
B0
C-1
D1
Attempts:
2 left
💡 Hint
strcmp returns 0 when strings are equal.
Predict Output
intermediate
2: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;
}
AA negative number
B0
CA positive number
DCompilation error
Attempts:
2 left
💡 Hint
strcmp returns negative if first string is less in lexicographic order.
Predict Output
advanced
2: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;
}
AA positive number
B0
CRuntime error
DA negative number
Attempts:
2 left
💡 Hint
Uppercase letters have lower ASCII codes than lowercase letters.
Predict Output
advanced
2: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;
}
AA negative number
B0
CA positive number
DSegmentation fault
Attempts:
2 left
💡 Hint
Empty string is less than any non-empty string.
Predict Output
expert
2: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;
}
A0
B1
C-1
DUndefined behavior
Attempts:
2 left
💡 Hint
strcmp compares character by character until difference found.