Common string operations - Time & Space Complexity
When working with strings in C, it's important to know how the time it takes to run your code changes as the string gets longer.
We want to find out how common string operations grow in cost as the input size grows.
Analyze the time complexity of the following code snippet.
#include <string.h>
int length = strlen(str);
char dest[100];
strcpy(dest, str);
int cmp = strcmp(str, dest);
This code finds the length of a string, copies it to another array, and then compares the two strings.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Each function (strlen, strcpy, strcmp) loops through the string characters one by one.
- How many times: Each function runs once, but each checks characters until it finds the string end.
As the string gets longer, each operation takes more steps because it looks at every character.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps per operation |
| 100 | About 100 steps per operation |
| 1000 | About 1000 steps per operation |
Pattern observation: The work grows directly with the string length; double the length, double the work.
Time Complexity: O(n)
This means the time to run these string operations grows in a straight line with the string size.
[X] Wrong: "String functions like strlen or strcpy run in constant time because they just look at the string once."
[OK] Correct: These functions actually check each character until they find the end, so their time depends on string length.
Understanding how string operations scale helps you write efficient code and answer questions about performance clearly.
"What if we used a function that only checked the first 5 characters of a string? How would the time complexity change?"
Practice
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]
- Confusing strcpy with strlen
- Using strcmp to get length
- Thinking strcat returns length
src into dest in C?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]
- Swapping source and destination arguments
- Using strcat instead of strcpy for copying
- Using strlen for copying strings
char str1[20] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
printf("%s", str1);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]
- Expecting strcat to replace string
- Confusing order of concatenation
- Assuming strcat returns new string
char s1[5] = "Hello";
char s2[10];
strcpy(s2, s1);
printf("%s", s2);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]
- Ignoring null terminator size
- Assuming strcpy reverses parameters
- Thinking s2 size is too small here
char text[] = "Programming in C";
char *ptr = strstr(text, "in");
printf("%s", ptr);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]
- Expecting strstr to return only substring
- Confusing strstr with strcmp
- Assuming strstr returns NULL if substring found
Ready to prove it?
Pass the test and this pattern moves unknown
