What is the purpose of the strlen() function in C?
The strlen() function returns the length of a string, not counting the null character \0 at the end.
Click to reveal answer
beginner
How does strcpy() function work in C?
strcpy() copies the content of one string (source) into another string (destination), including the null terminator.
Click to reveal answer
beginner
What does strcmp() return when two strings are equal?
strcmp() returns 0 if the two strings are exactly the same.
Click to reveal answer
intermediate
Explain the difference between strcat() and strncat().
strcat() appends one string to another until it hits the null character. strncat() appends up to a specified number of characters, helping to avoid buffer overflow.
Click to reveal answer
beginner
Why is it important to include string.h when using string library functions?
Including string.h provides the declarations of string handling functions so the compiler knows how to use them correctly.
Click to reveal answer
Which function is used to find the length of a string in C?
Astrcpy()
Bstrcmp()
Cstrlen()
Dstrcat()
✗ Incorrect
strlen() returns the number of characters in a string excluding the null terminator.
What does strcpy(dest, src) do?
AReturns length of src
BCompares dest and src strings
CConcatenates src to dest
DCopies src string into dest string
✗ Incorrect
strcpy() copies the source string into the destination string.
If strcmp(s1, s2) returns a negative value, what does it mean?
As1 is lexicographically less than s2
Bs1 is equal to s2
Cs1 is lexicographically greater than s2
DThere is an error
✗ Incorrect
A negative return means s1 comes before s2 in dictionary order.
Which function safely appends a limited number of characters from one string to another?
Astrncat()
Bstrcpy()
Cstrlen()
Dstrcmp()
✗ Incorrect
strncat() appends up to a specified number of characters, preventing buffer overflow.
What header file must be included to use string handling functions like strlen() and strcpy()?
Astdio.h
Bstring.h
Cmath.h
Dstdlib.h
✗ Incorrect
The string.h header declares all standard string handling functions.
Describe how to copy one string into another using C library functions. What should you be careful about?
Think about how strcpy works and what can go wrong if the destination is too small.
You got /4 concepts.
Explain how to compare two strings in C and interpret the result of the comparison.
Consider what strcmp returns when strings are equal or different.
You got /3 concepts.
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
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.
Step 2: Compare with other functions
strcpy() copies strings, strcmp() compares strings, strcat() concatenates strings, so they do not measure length.
Final Answer:
strlen() -> Option D
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
Step 1: Recall strcpy() function signature
strcpy() takes two arguments: destination string first, then source string to copy from.
Step 2: Check each option
Only strcpy(dest, src); matches the correct order and argument count.
Final Answer:
strcpy(dest, src); -> Option B
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?
s1 has size 5, holding "Data" plus '\0'. Concatenating "Science" (7 chars) will overflow s1.
Step 2: Verify other options
strcat() syntax is correct, #include <stdio.h> is not mandatory here, and s2 declaration is valid as array.
Final Answer:
s1 array size is too small for concatenation -> Option C
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
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.
Step 2: Apply to given strings
"apple" comes before "apricot", so strcmp(s1, s2) returns negative, meaning strcmp(s1, s2) < 0 is true.
Final Answer:
strcmp(s1, s2) < 0 -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.