Bird
Raised Fist0
Cprogramming~10 mins

String handling using library functions - 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 using a standard library function.

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

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

Complete the code to find the length of the string using a standard library function.

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

int main() {
    char str[] = "Hello, world!";
    int length = [1](str);
    printf("Length: %d", length);
    return 0;
}
Drag options to blanks, or click blank then click option'
Astrlen
Bstrcpy
Cstrcmp
Dstrcat
Attempts:
3 left
💡 Hint
Common Mistakes
Using strcpy which copies strings, not length.
Using strcmp which compares strings, not length.
Using strcat which concatenates strings, not length.
3fill in blank
hard

Fix the error in the code to compare two strings correctly using a standard library function.

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

int main() {
    char str1[] = "apple";
    char str2[] = "apple";
    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'
Astrcpy
Bstrcmp
Cstrlen
Dstrcat
Attempts:
3 left
💡 Hint
Common Mistakes
Using strcpy which copies strings, not compares.
Using strlen which returns length, not compares.
Using strcat which concatenates strings, not compares.
4fill in blank
hard

Fill both blanks to concatenate two strings safely using a standard library function.

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

int main() {
    char str1[20] = "Hello, ";
    char str2[] = "World!";
    [1](str1, str2, [2]);
    printf("%s", str1);
    return 0;
}
Drag options to blanks, or click blank then click option'
Astrncat
Bstrcpy
Csizeof(str1) - strlen(str1) - 1
Dstrlen(str2)
Attempts:
3 left
💡 Hint
Common Mistakes
Using strcpy which overwrites instead of appending.
Using strlen(str2) which may cause overflow if str1 is small.
Not calculating buffer size correctly.
5fill in blank
hard

Fill all three blanks to create a substring by copying part of a string using standard library functions.

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

int main() {
    char source[] = "Programming";
    char substring[10];
    strncpy(substring, source + [1], [2]);
    substring[[3]] = '\0';
    printf("%s", substring);
    return 0;
}
Drag options to blanks, or click blank then click option'
A3
B6
D7
Attempts:
3 left
💡 Hint
Common Mistakes
Not adding the null terminator, causing garbage output.
Copying wrong number of characters.
Starting from wrong index.

Practice

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

Solution

  1. Step 1: Understand the purpose of strlen()

    The function strlen() counts characters in a string until it reaches the null terminator '\0', giving the string length.
  2. Step 2: Compare with other functions

    strcpy() copies strings, strcmp() compares strings, strcat() concatenates strings, so they do not measure length.
  3. Final Answer:

    strlen() -> Option D
  4. Quick Check:

    Length function = strlen() [OK]
Hint: Length means count chars before '\0', use strlen() [OK]
Common Mistakes:
  • Confusing strcpy() with strlen()
  • Using strcmp() to get length
  • Forgetting null terminator in length count
2. Which of the following is the correct syntax to copy string src into dest using a standard library function?
easy
A. strcpy(src, dest);
B. strcpy(dest, src);
C. strcpy(dest);
D. strcpy(src);

Solution

  1. Step 1: Recall strcpy() function signature

    strcpy() takes two arguments: destination string first, then source string to copy from.
  2. Step 2: Check each option

    Only strcpy(dest, src); matches the correct order and argument count.
  3. Final Answer:

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

    Copy syntax = strcpy(dest, src) [OK]
Hint: Destination first, source second in strcpy() [OK]
Common Mistakes:
  • Swapping source and destination arguments
  • Using only one argument
  • Missing semicolon or parentheses
3. What will be the output of the following C code?
#include <stdio.h>
#include <string.h>

int main() {
    char str1[20] = "Hello";
    char str2[] = "World";
    strcat(str1, str2);
    printf("%s", str1);
    return 0;
}
medium
A. HelloWorld
B. Hello World
C. WorldHello
D. Hello

Solution

  1. Step 1: Understand strcat() behavior

    strcat() appends the second string to the end of the first string without adding spaces.
  2. Step 2: Analyze the code output

    str1 starts as "Hello", str2 is "World". After strcat(str1, str2), str1 becomes "HelloWorld".
  3. Final Answer:

    HelloWorld -> Option A
  4. Quick Check:

    Concatenate without space = HelloWorld [OK]
Hint: strcat joins strings directly, no spaces added [OK]
Common Mistakes:
  • Expecting a space between strings
  • Confusing strcat() with strcpy()
  • Not allocating enough space in destination
4. Identify the error in the following code snippet:
#include <string.h>

int main() {
    char s1[5] = "Data";
    char s2[] = "Science";
    strcat(s1, s2);
    return 0;
}
medium
A. Missing #include <stdio.h>
B. strcat() syntax is incorrect
C. s1 array size is too small for concatenation
D. s2 should be declared as char *s2

Solution

  1. Step 1: Check array sizes for strcat()

    s1 has size 5, holding "Data" plus '\0'. Concatenating "Science" (7 chars) will overflow s1.
  2. Step 2: Verify other options

    strcat() syntax is correct, #include <stdio.h> is not mandatory here, and s2 declaration is valid as array.
  3. Final Answer:

    s1 array size is too small for concatenation -> Option C
  4. Quick Check:

    Buffer overflow risk = s1 too small [OK]
Hint: Destination array must fit combined strings [OK]
Common Mistakes:
  • Ignoring buffer size for strcat()
  • Assuming strcat() adds space automatically
  • Confusing array and pointer declarations
5. Given two strings char s1[] = "apple"; and char s2[] = "apricot";, which library function call correctly checks if s1 comes before s2 in dictionary order?
hard
A. strcmp(s1, s2) < 0
B. strcmp(s1, s2) > 0
C. strcmp(s2, s1) < 0
D. strcmp(s2, s1) > 0

Solution

  1. Step 1: Understand strcmp() return values

    strcmp() returns a negative value if first string is lexicographically less than second, zero if equal, positive if greater.
  2. Step 2: Apply to given strings

    "apple" comes before "apricot", so strcmp(s1, s2) returns negative, meaning strcmp(s1, s2) < 0 is true.
  3. Final Answer:

    strcmp(s1, s2) < 0 -> Option A
  4. Quick Check:

    Lex order check = strcmp(s1, s2) < 0 [OK]
Hint: strcmp() negative means first string is before second [OK]
Common Mistakes:
  • Reversing argument order in strcmp()
  • Checking for > 0 instead of < 0
  • Assuming strcmp() returns boolean
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