Bird
Raised Fist0
Cprogramming~10 mins

String comparison - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to compare two strings using the correct function.

C
#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "apple";
    char str2[] = "apple";
    if ([1](str1, str2) == 0) {
        printf("Strings are equal.\n");
    } else {
        printf("Strings are not equal.\n");
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Astrcmp
Bstrcpy
Cstrlen
Dstrcat
Attempts:
3 left
💡 Hint
Common Mistakes
Using strcpy instead of strcmp.
Using strlen which returns length, not comparison.
2fill in blank
medium

Complete the code to check if two strings are different.

C
#include <stdio.h>
#include <string.h>

int main() {
    char a[] = "cat";
    char b[] = "dog";
    if ([1](a, b) != 0) {
        printf("Strings are different.\n");
    } else {
        printf("Strings are the same.\n");
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Astrcmp
Bstrcpy
Cstrlen
Dstrchr
Attempts:
3 left
💡 Hint
Common Mistakes
Using strcpy which copies strings.
Using strlen which returns length.
3fill in blank
hard

Fix the error in the code to correctly compare strings ignoring case.

C
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
    char s1[] = "Hello";
    char s2[] = "hello";
    if ([1](s1, s2) == 0) {
        printf("Strings are equal ignoring case.\n");
    } else {
        printf("Strings are different.\n");
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Astrcmp
Bstrncmp
Cstrcasecmp
Dstrcpy
Attempts:
3 left
💡 Hint
Common Mistakes
Using strcmp which is case sensitive.
Using strcpy which copies strings.
4fill in blank
hard

Fill both blanks to create a dictionary-like structure that maps words to their lengths if length is greater than 3.

C
#include <stdio.h>
#include <string.h>

int main() {
    char *words[] = {"apple", "cat", "banana", "dog"};
    int lengths[4];
    for (int i = 0; i < 4; i++) {
        if ([1](words[i]) [2] 3) {
            lengths[i] = strlen(words[i]);
            printf("%s: %d\n", words[i], lengths[i]);
        }
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Astrlen
B>
C<
Dstrcmp
Attempts:
3 left
💡 Hint
Common Mistakes
Using strcmp instead of strlen for length.
Using less than operator instead of greater than.
5fill in blank
hard

Fill all three blanks to create a filtered dictionary of words with length greater than 4, mapping uppercase words to their lengths.

C
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void to_upper(char *dest, const char *src) {
    int i = 0;
    while (src[i]) {
        dest[i] = [1](src[i]);
        i++;
    }
    dest[i] = '\0';
}

int main() {
    char *words[] = {"apple", "cat", "banana", "dog"};
    char upper[20];
    int lengths[4];
    for (int i = 0; i < 4; i++) {
        if (strlen(words[i]) [2] 4) {
            to_upper(upper, words[i]);
            lengths[i] = strlen(words[i]);
            printf("%s: %d\n", [3], lengths[i]);
        }
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Atoupper
B>
Cupper
Dtolower
Attempts:
3 left
💡 Hint
Common Mistakes
Using tolower instead of toupper.
Printing the original word instead of the uppercase version.

Practice

(1/5)
1. Which function is used in C to compare the contents of two strings?
easy
A. strcpy
B. strlen
C. strcmp
D. strcat

Solution

  1. Step 1: Identify string comparison functions

    Among the options, only strcmp is designed to compare two strings character by character.
  2. Step 2: Understand other functions

    strcpy copies strings, strlen measures length, strcat concatenates strings; none compare contents.
  3. Final Answer:

    strcmp -> Option C
  4. Quick Check:

    strcmp compares strings [OK]
Hint: Use strcmp to compare strings, not == operator [OK]
Common Mistakes:
  • Using == to compare strings
  • Confusing strcpy with strcmp
  • Assuming strlen compares strings
2. Which header file must be included to use the strcmp function in C?
easy
A.
B.
C.
D.

Solution

  1. Step 1: Recall standard headers for string functions

    String functions like strcmp are declared in the <string.h> header file.
  2. Step 2: Verify other headers

    <stdio.h> is for input/output, <stdlib.h> for general utilities, <ctype.h> for character handling, none provide strcmp.
  3. Final Answer:

    <string.h> -> Option D
  4. Quick Check:

    strcmp needs <string.h> [OK]
Hint: Include for strcmp and other string functions [OK]
Common Mistakes:
  • Forgetting to include
  • Including only
  • Using wrong header like
3. What is the output of the following C code?
#include <stdio.h>
#include <string.h>

int main() {
    char a[] = "apple";
    char b[] = "apricot";
    int result = strcmp(a, b);
    if (result < 0) {
        printf("a is less than b\n");
    } else if (result > 0) {
        printf("a is greater than b\n");
    } else {
        printf("a and b are equal\n");
    }
    return 0;
}
medium
A. a is less than b
B. a is greater than b
C. a and b are equal
D. Compilation error

Solution

  1. Step 1: Understand strcmp result for "apple" and "apricot"

    strcmp compares strings lexicographically; "apple" is less than "apricot" because 'l' (in apple) comes before 'r' (in apricot).
  2. Step 2: Check the if conditions

    Since result is negative, the program prints "a is less than b".
  3. Final Answer:

    a is less than b -> Option A
  4. Quick Check:

    strcmp("apple", "apricot") < 0 [OK]
Hint: strcmp returns negative if first string is lexically smaller [OK]
Common Mistakes:
  • Assuming strcmp returns boolean
  • Confusing lex order of 'l' and 'r'
  • Expecting equality for different strings
4. Identify the error in this code snippet comparing two strings:
#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "hello";
    char str2[] = "hello";
    if (str1 == str2) {
        printf("Strings are equal\n");
    } else {
        printf("Strings are not equal\n");
    }
    return 0;
}
medium
A. Using '==' to compare strings instead of strcmp
B. Missing #include header
C. Incorrect printf format specifier
D. Strings are not initialized properly

Solution

  1. Step 1: Understand string comparison in C

    Using '==' compares addresses, not string contents, so it does not check if strings are equal.
  2. Step 2: Correct method to compare strings

    Use strcmp(str1, str2) == 0 to check if strings have the same content.
  3. Final Answer:

    Using '==' to compare strings instead of strcmp -> Option A
  4. Quick Check:

    Use strcmp for string content comparison [OK]
Hint: Never use '==' for strings; use strcmp instead [OK]
Common Mistakes:
  • Using '==' operator for string comparison
  • Ignoring string content vs pointer comparison
  • Not including when using strcmp
5. You want to check if two strings str1 and str2 are equal ignoring case differences in C. Which approach correctly achieves this?
hard
A. Use strcmp(str1, str2) == 0
B. Use strcasecmp(str1, str2) == 0
C. Use strcmp(tolower(str1), tolower(str2)) == 0
D. Use strcmp(str1, str2) != 0

Solution

  1. Step 1: Understand case-insensitive comparison

    strcmp compares case-sensitively, so it fails if cases differ. strcasecmp compares ignoring case.
  2. Step 2: Evaluate other options

    Use strcmp(str1, str2) == 0 is case-sensitive; Use strcmp(tolower(str1), tolower(str2)) == 0 is invalid because tolower expects a char, not a string; Use strcmp(str1, str2) != 0 checks inequality, not equality.
  3. Final Answer:

    Use strcasecmp(str1, str2) == 0 -> Option B
  4. Quick Check:

    strcasecmp compares strings ignoring case [OK]
Hint: Use strcasecmp for case-insensitive string comparison [OK]
Common Mistakes:
  • Using strcmp for case-insensitive check
  • Applying tolower to whole strings incorrectly
  • Confusing equality and inequality checks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard