What function is used to find the length of a string in C?
The strlen() function from string.h returns the number of characters in a string, excluding the null terminator.
Click to reveal answer
beginner
How do you copy one string to another in C?
Use the strcpy() function to copy the content of one string into another. Make sure the destination has enough space.
Click to reveal answer
beginner
What does strcmp() do in C?
strcmp() compares two strings and returns 0 if they are equal, a negative value if the first is less, or a positive value if the first is greater.
Click to reveal answer
beginner
How can you concatenate two strings in C?
Use strcat() to append one string to the end of another. The destination string must have enough space to hold the result.
Click to reveal answer
intermediate
Why must you be careful with string operations in C?
Because C strings are arrays of characters ending with a null byte, you must ensure enough memory is allocated to avoid buffer overflows and undefined behavior.
Click to reveal answer
Which function returns the length of a string in C?
Astrcpy()
Bstrlen()
Cstrcmp()
Dstrcat()
✗ Incorrect
strlen() counts characters until the null terminator.
What does strcmp() return when two strings are equal?
AThe length of the strings
BA negative number
CA positive number
DZero
✗ Incorrect
strcmp() returns 0 if strings are exactly the same.
Which function appends one string to another in C?
Astrcat()
Bstrcpy()
Cstrlen()
Dstrcmp()
✗ Incorrect
strcat() adds the second string to the end of the first.
What must you ensure before using strcpy()?
AThe destination has enough space
BThe strings are the same length
CThe source string is empty
DThe strings are null-terminated
✗ Incorrect
The destination must have enough space to hold the copied string.
What character marks the end of a string in C?
ASpace character
BNewline character
CNull character '\0'
DTab character
✗ Incorrect
Strings in C end with the null character '\0' to mark their end.
Explain how to safely copy and concatenate strings in C.
Think about destination size and string endings.
You got /4 concepts.
Describe what strcmp() returns and how to interpret its result.
Compare two strings alphabetically.
You got /3 concepts.
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
Step 1: Understand string length concept
The length of a string is the number of characters before the null terminator.
Step 2: Identify the correct function
strlen returns the length of a string, while strcpy copies, strcmp compares, and strcat concatenates strings.