📖 Scenario: You are working on a simple program that processes a user's name. You want to learn how to use C library functions to handle strings easily.
🎯 Goal: Build a program that stores a name, calculates its length, copies it to another string, and compares it with another name using C string library functions.
📋 What You'll Learn
Create a character array called name with the exact value "Alice".
Create an integer variable called length to store the length of name.
Create a character array called copy to hold a copy of name.
Create a character array called other with the exact value "Bob".
Use strlen to find the length of name.
Use strcpy to copy name into copy.
Use strcmp to compare name and other.
Print the length of name, the copied string, and the comparison result.
💡 Why This Matters
🌍 Real World
String handling is essential in programs that process text, like user names, messages, or file paths.
💼 Career
Many software jobs require manipulating strings safely and efficiently using standard library functions.
Progress0 / 4 steps
1
DATA SETUP: Create the initial string variable
Create a character array called name and set it to the string "Alice".
C
Hint
Use char name[] = "Alice"; to create the string.
2
CONFIGURATION: Add variables for length and another string
Add an integer variable called length and a character array called copy with size 10.
C
Hint
Use int length; and char copy[10]; to declare the variables.
3
CORE LOGIC: Use library functions to get length and copy string
Use strlen to set length to the length of name. Use strcpy to copy name into copy. Also create a character array called other with the value "Bob" and use strcmp to compare name and other, storing the result in an integer variable called cmp.
C
Hint
Use length = strlen(name);, strcpy(copy, name);, and cmp = strcmp(name, other);.
4
OUTPUT: Print the results
Print the length of name using printf with the message Name length: . Print the copied string copy with the message Copied name: . Print the comparison result cmp with the message Comparison result: .
C
Hint
Use printf to show the length, copied string, and comparison result.
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.