Strings are used to store text. Common operations help us read, change, and compare text easily.
Common string operations
Introduction
Syntax
C
strlen(string) // Returns length of string strcpy(dest, src) // Copies src string to dest strcat(dest, src) // Adds src string at the end of dest strcmp(str1, str2) // Compares two strings strstr(haystack, needle) // Finds needle in haystack
All strings in C end with a special character '\0' to mark the end.
Make sure destination arrays have enough space before copying or adding strings.
Examples
C
char name[20] = "Alice"; int length = strlen(name);
name to copy.C
char copy[20]; strcpy(copy, name);
C
char greeting[30] = "Hello, "; strcat(greeting, name);
name is exactly "Alice".C
if (strcmp(name, "Alice") == 0) { // strings are equal }
Sample Program
This program joins two words with a space, prints the combined string and its length, then checks if the word "World" is inside the combined string.
C
#include <stdio.h> #include <string.h> int main() { char first[20] = "Hello"; char second[20] = "World"; char combined[50]; // Copy first into combined strcpy(combined, first); // Add a space strcat(combined, " "); // Add second into combined strcat(combined, second); printf("Combined string: %s\n", combined); printf("Length: %zu\n", strlen(combined)); // Check if combined contains "World" if (strstr(combined, "World") != NULL) { printf("Found 'World' inside combined.\n"); } else { printf("Did not find 'World'.\n"); } return 0; }
Important Notes
Always include <string.h> to use these string functions.
Be careful with buffer sizes to avoid errors or crashes.
Strings in C are arrays of characters ending with '\0'.
Summary
Use strlen to get string length.
Use strcpy to copy strings safely.
Use strcat to join strings.
Use strcmp to compare strings.
Use strstr to find a substring inside another string.
Practice
1. Which C function is used to find the length of a string?
easy
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
strlenreturns the length of a string, whilestrcpycopies,strcmpcompares, andstrcatconcatenates strings.Final Answer:
strlen -> Option AQuick 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
Solution
Step 1: Understand strcpy parameters
strcpycopies the source string into the destination string. The first argument is destination, second is source.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.Final Answer:
strcpy(dest, src); -> Option DQuick 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
Solution
Step 1: Understand strcat behavior
strcatappends the second string to the end of the first string.Step 2: Apply strcat to given strings
Appending "World!" to "Hello, " results in "Hello, World!" stored instr1.Final Answer:
Hello, World! -> Option CQuick 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
Solution
Step 1: Check array size for s1
String "Hello" has 5 characters plus 1 for '\0' terminator, so s1 needs size at least 6.Step 2: Analyze s1 declaration
s1 is declared with size 5, which is too small to hold "Hello" plus null terminator, causing overflow.Final Answer:
s1 array size is too small for "Hello" -> Option BQuick 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
Solution
Step 1: Understand strstr function
strstrreturns pointer to first occurrence of substring or NULL if not found.Step 2: Find substring "in" in text
"in" first appears inside "Programming in C" starting at "in C" substring.Step 3: Print from pointer
Printing from pointer prints "in C" including the substring and rest of string.Final Answer:
in C -> Option AQuick 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
10 questions ~5 mineasy hard
