String Traversal and Character Access in DSA C - Time & Space Complexity
When we look at how fast a program runs that reads each letter in a word, we want to know how the time changes if the word gets longer.
We ask: How does the time to check all letters grow as the word grows?
Analyze the time complexity of the following code snippet.
#include <stdio.h>
void printChars(char *str) {
int i = 0;
while (str[i] != '\0') {
printf("%c ", str[i]);
i++;
}
}
int main() {
char word[] = "hello";
printChars(word);
return 0;
}
This code goes through each letter in a word and prints it one by one until it reaches the end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each character in the string.
- How many times: Once for every character until the end of the string.
As the word gets longer, the program checks more letters one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and prints |
| 100 | About 100 checks and prints |
| 1000 | About 1000 checks and prints |
Pattern observation: The number of steps grows directly with the number of letters.
Time Complexity: O(n)
This means the time to run grows in a straight line with the length of the string.
[X] Wrong: "Accessing each character is instant and does not add up."
[OK] Correct: Each character must be checked one by one, so the total time adds up as the string gets longer.
Understanding how reading each letter takes time helps you explain how programs handle text efficiently, a useful skill in many coding tasks.
"What if we changed the loop to only check every second character? How would the time complexity change?"
Practice
str[0] represent when str is a string in C?Solution
Step 1: Understand string indexing in C
In C, strings are arrays of characters. Indexing starts at 0, sostr[0]accesses the first character.Step 2: Identify what
It returns the character stored at the first position of the string, not the length or pointer.str[0]returnsFinal Answer:
The first character of the string -> Option AQuick Check:
str[0] = first character [OK]
- Confusing character access with string length
- Thinking indexing starts at 1
- Mixing pointer and character access
str of length n in C using a for loop?Solution
Step 1: Understand string indexing and loop bounds
String indices in C start at 0 and go up to n-1 for length n.Step 2: Choose loop condition to cover all characters
The loop should run from 0 to n-1, soi < nis correct.Final Answer:
for (int i = 0; i < n; i++) -> Option CQuick Check:
Loop from 0 to n-1 = for (i=0; i<n; i++) [OK]
- Using <= n causes out-of-bounds access
- Starting loop at 1 misses first character
- Confusing string length with last index
char str[] = "hello";
for (int i = 0; i < 5; i++) {
printf("%c-", str[i]);
}Solution
Step 1: Analyze the loop and print statement
The loop runs from i=0 to 4, printing each character followed by a dash.Step 2: Trace each iteration output
Characters printed: 'h-', 'e-', 'l-', 'l-', 'o-'. Combined output is "h-e-l-l-o-".Final Answer:
h-e-l-l-o- -> Option BQuick Check:
Each char + '-' printed = h-e-l-l-o- [OK]
- Ignoring the dash after each character
- Printing the whole string once instead of chars
- Confusing spaces with dashes
str.char str[] = "world";
for (int i = 1; i <= 5; i++) {
printf("%c", str[i]);
}Solution
Step 1: Check loop start index
Loop starts at 1, so it skipsstr[0]which is the first character.Step 2: Check loop end condition
Loop runs while i <= 5, but valid indices are 0 to 4. Accessingstr[5]causes out-of-bounds error.Final Answer:
Both A and B are errors -> Option AQuick Check:
Start at 0 and use i < length to avoid errors [OK]
- Starting loop at 1 and missing first char
- Using <= length causing out-of-bounds access
- Ignoring array indexing rules
str of unknown length, which of the following code snippets correctly prints all characters until the null terminator in C?Solution
Step 1: Analyze for (int i = 0; str[i] != '\0'; i++) { printf("%c", str[i]); }
Loops until character is not null terminator, printing each character. Correct method.Step 2: Analyze for (int i = 0; i < strlen(str); i++) { printf("%c", str[i]); } and C
for (int i = 0; i < strlen(str); i++) { printf("%c", str[i]); } usesstrlento get length, then loops printing characters. while (*str != '\0') { printf("%c", *str); str++; } uses pointer traversal until null terminator. Both are correct.Step 3: Combine results
All three methods correctly print the string characters until the null terminator.Final Answer:
All of the above -> Option DQuick Check:
All methods stop at '\0' and print chars [OK]
- Not checking for null terminator causing overflow
- Using wrong loop conditions
- Modifying string pointer incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
