Bird
Raised Fist0
Cprogramming~10 mins

Common string operations - Interactive Code Practice

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

Complete the code to copy the string from source to destination.

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

int main() {
    char source[] = "Hello";
    char destination[10];
    [1](destination, source);
    printf("%s\n", destination);
    return 0;
}
Drag options to blanks, or click blank then click option'
Astrcpy
Bstrlen
Cstrcmp
Dstrcat
Attempts:
3 left
💡 Hint
Common Mistakes
Using strlen instead of strcpy will cause errors because strlen returns length, not copy.
Using strcmp compares strings but does not copy.
Using strcat appends strings instead of copying.
2fill in blank
medium

Complete the code to find the length of the string text.

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

int main() {
    char text[] = "Programming";
    int length = [1](text);
    printf("Length: %d\n", length);
    return 0;
}
Drag options to blanks, or click blank then click option'
Astrcpy
Bstrcat
Cstrcmp
Dstrlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using strcpy will copy strings but not give length.
Using strcmp compares strings but does not give length.
Using strcat concatenates strings but does not give length.
3fill in blank
hard

Fix the error in the code to compare two strings str1 and str2.

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

int main() {
    char str1[] = "apple";
    char str2[] = "orange";
    if ([1](str1, str2) == 0) {
        printf("Strings are equal\n");
    } else {
        printf("Strings are different\n");
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Astrlen
Bstrcmp
Cstrcpy
Dstrcat
Attempts:
3 left
💡 Hint
Common Mistakes
Using strcpy copies strings but does not compare.
Using strlen returns length, not comparison result.
Using strcat concatenates strings, not compares.
4fill in blank
hard

Fill both blanks to concatenate suffix to base and print the result.

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

int main() {
    char base[20] = "file";
    char suffix[] = ".txt";
    [1](base, suffix);
    printf("%s\n", [2]);
    return 0;
}
Drag options to blanks, or click blank then click option'
Astrcat
Bstrcpy
Cbase
Dsuffix
Attempts:
3 left
💡 Hint
Common Mistakes
Using strcpy instead of strcat will overwrite base.
Printing suffix instead of base will not show the combined string.
5fill in blank
hard

Fill all three blanks to create a new string result that contains the uppercase version of word and print its length.

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

int main() {
    char word[] = "hello";
    char result[10];
    for (int i = 0; i < strlen(word); i++) {
        result[i] = [1](word[i]);
    }
    result[strlen(word)] = '\0';
    printf("%s\n", [2]);
    printf("Length: %d\n", [3](result));
    return 0;
}
Drag options to blanks, or click blank then click option'
Atoupper
Bresult
Cstrlen
Dtolower
Attempts:
3 left
💡 Hint
Common Mistakes
Using tolower will convert letters to lowercase, not uppercase.
Printing word instead of result will show the original string.
Using strlen on word instead of result will not reflect changes.

Practice

(1/5)
1. Which C function is used to find the length of a string?
easy
A. strlen
B. strcpy
C. strcmp
D. strcat

Solution

  1. Step 1: Understand string length concept

    The length of a string is the number of characters before the null terminator.
  2. Step 2: Identify the correct function

    strlen returns the length of a string, while strcpy copies, strcmp compares, and strcat concatenates strings.
  3. Final Answer:

    strlen -> Option A
  4. Quick Check:

    Length function = strlen [OK]
Hint: Remember: length = strlen, copy = strcpy [OK]
Common Mistakes:
  • Confusing strcpy with strlen
  • Using strcmp to get length
  • Thinking strcat returns length
2. Which of the following is the correct syntax to copy string src into dest in C?
easy
A. strcat(dest, src);
B. strcpy(src, dest);
C. strlen(dest, src);
D. strcpy(dest, src);

Solution

  1. Step 1: Understand strcpy parameters

    strcpy copies the source string into the destination string. The first argument is destination, second is source.
  2. Step 2: Check each option

    strcpy(dest, src); uses correct order (destination first, source second). strcpy(src, dest); reverses parameters. strcat(dest, src); concatenates strings. strlen(dest, src); returns length, not copies.
  3. Final Answer:

    strcpy(dest, src); -> Option D
  4. Quick Check:

    Copy syntax = strcpy(dest, src) [OK]
Hint: Copy: destination first, source second in strcpy [OK]
Common Mistakes:
  • Swapping source and destination arguments
  • Using strcat instead of strcpy for copying
  • Using strlen for copying strings
3. What is the output of this C code?
char str1[20] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
printf("%s", str1);
medium
A. Hello,
B. World!Hello,
C. Hello, World!
D. Compilation error

Solution

  1. Step 1: Understand strcat behavior

    strcat appends the second string to the end of the first string.
  2. Step 2: Apply strcat to given strings

    Appending "World!" to "Hello, " results in "Hello, World!" stored in str1.
  3. Final Answer:

    Hello, World! -> Option C
  4. Quick Check:

    Concatenate str1 + str2 = Hello, World! [OK]
Hint: strcat adds second string to end of first [OK]
Common Mistakes:
  • Expecting strcat to replace string
  • Confusing order of concatenation
  • Assuming strcat returns new string
4. Identify the error in this code snippet:
char s1[5] = "Hello";
char s2[10];
strcpy(s2, s1);
printf("%s", s2);
medium
A. s2 array size is too small for copy
B. s1 array size is too small for "Hello"
C. strcpy parameters are reversed
D. Missing null terminator in s2

Solution

  1. Step 1: Check array size for s1

    String "Hello" has 5 characters plus 1 for '\0' terminator, so s1 needs size at least 6.
  2. Step 2: Analyze s1 declaration

    s1 is declared with size 5, which is too small to hold "Hello" plus null terminator, causing overflow.
  3. Final Answer:

    s1 array size is too small for "Hello" -> Option B
  4. Quick Check:

    String size must include '\0' [OK]
Hint: String size must fit characters + '\0' terminator [OK]
Common Mistakes:
  • Ignoring null terminator size
  • Assuming strcpy reverses parameters
  • Thinking s2 size is too small here
5. Given the code below, what will be printed?
char text[] = "Programming in C";
char *ptr = strstr(text, "in");
printf("%s", ptr);
hard
A. in C
B. Programming in C
C. in
D. NULL

Solution

  1. Step 1: Understand strstr function

    strstr returns pointer to first occurrence of substring or NULL if not found.
  2. Step 2: Find substring "in" in text

    "in" first appears inside "Programming in C" starting at "in C" substring.
  3. Step 3: Print from pointer

    Printing from pointer prints "in C" including the substring and rest of string.
  4. Final Answer:

    in C -> Option A
  5. Quick Check:

    strstr returns substring start pointer [OK]
Hint: strstr returns pointer to substring start [OK]
Common Mistakes:
  • Expecting strstr to return only substring
  • Confusing strstr with strcmp
  • Assuming strstr returns NULL if substring found
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