0
0
Cprogramming~10 mins

String comparison - Interactive Code Practice

Choose your learning style9 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.