0
0
Cprogramming~5 mins

String comparison - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A-1
B0
C1
DThe length of the strings
Which header file is required to use strcmp() in C?
A<code>&lt;string.h&gt;</code>
B<code>&lt;stdio.h&gt;</code>
C<code>&lt;stdlib.h&gt;</code>
D<code>&lt;math.h&gt;</code>
Why is using == to compare strings in C usually incorrect?
AIt compares memory addresses, not content
BIt converts strings to integers
CIt causes a syntax error
DIt compares string lengths
If strcmp(str1, str2) returns a positive number, what does it mean?
AStrings are equal
B<code>str1</code> is lexicographically less than <code>str2</code>
C<code>str1</code> is lexicographically greater than <code>str2</code>
DAn error occurred
Which of these is the correct way to check if two strings a and b are equal?
Aif (a == b)
Bif (strcmp(a, b) != 0)
Cif (a > b)
Dif (strcmp(a, b) == 0)
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.