How Strings Work Differently Across Languages in DSA C - Complexity Walkthrough
When working with strings, how fast operations run depends on how strings are handled inside the language.
We want to see how string operations grow as the string gets longer.
Analyze the time complexity of the following code snippet.
#include <stdio.h>
#include <string.h>
void print_chars(const char *str) {
int len = strlen(str);
for (int i = 0; i < len; i++) {
printf("%c ", str[i]);
}
printf("\n");
}
int main() {
print_chars("hello world");
return 0;
}
This code prints each character of a string one by one using a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each character of the string.
- How many times: Exactly once for each character in the string (length n).
As the string gets longer, the number of steps grows directly with its length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character prints |
| 100 | About 100 character prints |
| 1000 | About 1000 character prints |
Pattern observation: The work grows in a straight line as the string length increases.
Time Complexity: O(n)
This means the time to print characters grows directly with the string length.
[X] Wrong: "Accessing any character in a string is always O(1) no matter the language."
[OK] Correct: In C, strings are arrays so access is O(1), but in some languages like Python or JavaScript, strings may be immutable or encoded differently, making some operations slower.
Understanding how strings work under the hood helps you explain your code choices clearly and shows you know how performance changes with input size.
"What if the string was stored as a linked list of characters instead of an array? How would the time complexity of accessing characters change?"
Practice
Solution
Step 1: Understand C string representation
In C, strings are arrays of characters stored in contiguous memory locations.Step 2: Identify string termination
C strings end with a special null character '\0' to mark the end of the string.Final Answer:
As an array of characters ending with a null character '\0' -> Option BQuick Check:
C strings = char array + '\0' [OK]
- Thinking strings are objects with methods like in other languages
- Assuming strings are linked lists in C
- Ignoring the null terminator character
Solution
Step 1: Check declaration syntax for string literals in C
In C, strings are arrays of characters, so we declare with char array syntax.Step 2: Validate initialization
char str[] = "hello"; creates an array with characters 'h','e','l','l','o','\0'.Final Answer:
char str[] = "hello"; -> Option AQuick Check:
char array with double quotes = correct string declaration [OK]
- Using single quotes for strings instead of double quotes
- Trying to use 'string' type which doesn't exist in C
- Declaring char as a single character instead of array
char str[] = "abc";
printf("%c", str[3]);Solution
Step 1: Understand string indexing in C
String "abc" is stored as ['a','b','c','\0'] with indices 0 to 3.Step 2: Identify character at index 3
str[3] is the null terminator '\0', which prints no visible character.Final Answer:
\0 (null character, no visible output) -> Option AQuick Check:
Index 3 = '\0' null terminator [OK]
- Expecting str[3] to print 'c' instead of null
- Assuming runtime error due to out-of-bounds access
- Confusing zero-based indexing
char src[] = "hello";
char dest[5];
strcpy(dest, src);
printf("%s", dest);Solution
Step 1: Check size of destination array
"hello" has 5 characters plus '\0' terminator, total 6 chars needed.Step 2: Compare with dest array size
dest is declared with size 5, which is too small to hold full string plus '\0'.Final Answer:
dest array size is too small to hold the string and null terminator -> Option DQuick Check:
Destination must fit string + '\0' [OK]
- Ignoring null terminator space in array size
- Assuming strcpy needs special declaration
- Confusing %c and %s in printf
char str1[] = "test";
char *str2 = "test";
str1[0] = 'b';
// str2[0] = 'b'; // commented out
printf("%s %s", str1, str2);
Why is the line modifying str2 commented out?Solution
Step 1: Understand difference between array and pointer to string literal
str1 is a modifiable array copy of "test"; str2 points to a string literal in read-only memory.Step 2: Explain why modifying str2 causes error
Modifying string literals is undefined behavior and often causes runtime error, so line is commented out.Final Answer:
Because string literals pointed by str2 are stored in read-only memory and cannot be modified -> Option CQuick Check:
String literals are read-only, arrays are modifiable [OK]
- Thinking str2 is a constant pointer (it's pointer to const data)
- Assuming arrays cannot be modified
- Confusing pointer and array behavior
Ready to prove it?
Pass the test and this pattern moves unknown
