String Basics and Memory Representation in DSA C - Time & Space Complexity
We want to understand how the time to work with strings grows as the string gets longer.
Specifically, how does reading or copying a string change when the string size changes?
Analyze the time complexity of the following code snippet.
#include <stdio.h>
#include <string.h>
void copyString(char *dest, const char *src) {
int i = 0;
while (src[i] != '\0') {
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
This code copies a string from source to destination character by character until it reaches the end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Copying each character from source to destination.
- How many times: Once for every character in the string until the end character '\0'.
As the string gets longer, the number of characters copied grows directly with its length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character copies |
| 100 | About 100 character copies |
| 1000 | About 1000 character copies |
Pattern observation: The work grows in a straight line with the string length.
Time Complexity: O(n)
This means the time to copy the string grows directly with the number of characters in the string.
[X] Wrong: "Copying a string takes the same time no matter how long it is."
[OK] Correct: Each character must be copied one by one, so longer strings take more time.
Understanding how string operations scale helps you reason about performance in real programs and shows you know how memory and loops work together.
"What if we used a built-in function like strcpy instead of a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand C string storage
In C, strings are stored as arrays of characters with a special null character '\0' marking the end.Step 2: Compare options with this knowledge
Only As an array of characters ending with a null character '\0'. correctly states this; others describe different data structures or types.Final Answer:
As an array of characters ending with a null character '\0'. -> Option AQuick Check:
C strings end with '\0' = A [OK]
- Thinking strings are linked lists in C
- Assuming strings store length explicitly
- Confusing string storage with other data types
Solution
Step 1: Check string declaration syntax
In C, to store a string literal, use an array of chars with double quotes and square brackets.Step 2: Evaluate each option
A uses char (single char) with double quotes (wrong). B uses single quotes for multiple chars (wrong). D misses null terminator '\0'. C correctly declares and initializes with null terminator.Final Answer:
char str[] = "hello"; -> Option CQuick Check:
Use char array with double quotes = C [OK]
- Using single quotes for strings
- Not including null terminator
- Declaring char variable instead of array
char str[] = "abc";
printf("%c", str[3]);
Solution
Step 1: Understand string indexing and null terminator
The string "abc" is stored as {'a','b','c','\0'}. Index 3 is the null character '\0'.Step 2: Determine printf output for str[3]
Printing '%c' with '\0' prints nothing visible (null character), no error occurs.Final Answer:
\0 (null character, prints nothing visible) -> Option BQuick Check:
Index 3 is '\0' = A [OK]
- Assuming str[3] is 'a' or 'c'
- Expecting runtime error for accessing null char
- Confusing null char with printable character
char str[5] = "hello";
str[5] = '!';
Solution
Step 1: Analyze array size and indexing
Array str has size 5, valid indices are 0 to 4. Accessing str[5] is out of bounds.Step 2: Check string initialization and assignment
"hello" fits exactly 5 chars including '\0'? No, 'hello' needs 6 chars including '\0'. But main error is str[5] assignment which is invalid index.Final Answer:
Array index out of bounds when assigning str[5]. -> Option AQuick Check:
Index 5 invalid for size 5 array = D [OK]
- Ignoring out of bounds access
- Confusing array size with string length
- Thinking str[5] is valid index
char str1[] = "abc";
char *str2 = str1;
str2[1] = 'z';
printf("%s", str1);
Solution
Step 1: Understand pointer and array relation
str2 points to str1 array. Changing str2[1] changes str1[1].Step 2: Apply the character change and print
str2[1] = 'z' changes 'b' to 'z'. Printing str1 shows "azc".Final Answer:
azc -> Option DQuick Check:
Pointer modifies original array = B [OK]
- Thinking str1 remains unchanged
- Expecting null character insertion
- Assuming runtime error due to pointer
Ready to prove it?
Pass the test and this pattern moves unknown
