Recall & Review
beginner
What function is commonly used in C to compare two strings?
The
strcmp() function is used to compare two strings in C. It returns 0 if the strings are equal.Click to reveal answer
beginner
What does
strcmp() return if the first string is lexicographically less than the second?It returns a negative integer if the first string is less than the second string in dictionary order.
Click to reveal answer
beginner
How do you check if two strings are equal using
strcmp()?You check if
strcmp(str1, str2) == 0. If true, the strings are equal.Click to reveal answer
beginner
Why can't you use the
== operator to compare strings in C?Because
== compares the memory addresses (pointers) of the strings, not their content.Click to reveal answer
beginner
What header file must you include to use
strcmp()?You must include
<string.h> to use strcmp().Click to reveal answer
What does
strcmp(str1, str2) return if str1 and str2 are exactly the same?✗ Incorrect
strcmp() returns 0 when both strings are equal.
Which header file is required to use
strcmp() in C?✗ Incorrect
<string.h> contains the declaration for strcmp().
Why is using
== to compare strings in C usually incorrect?✗ Incorrect
== compares pointers (memory addresses), not the actual string content.
If
strcmp(str1, str2) returns a positive number, what does it mean?✗ Incorrect
A positive return means str1 comes after str2 in dictionary order.
Which of these is the correct way to check if two strings
a and b are equal?✗ Incorrect
Use strcmp(a, b) == 0 to check if strings are equal.
Explain how to compare two strings in C and why you cannot use the
== operator.Think about what <code>==</code> does with pointers.
You got /3 concepts.
Describe the meaning of the return values of
strcmp() when comparing two strings.Consider dictionary order comparison.
You got /3 concepts.