String input and output in C - Time & Space Complexity
When working with strings in C, it's important to know how the time to read or print a string grows as the string gets longer.
We want to understand how the program's work changes when the input string size changes.
Analyze the time complexity of the following code snippet.
#include <stdio.h>
int main() {
char str[1000];
scanf("%999s", str); // Read a string safely
printf("%s", str); // Print the string
return 0;
}
This code reads a string from the user and then prints it back to the screen.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading and printing each character of the string one by one.
- How many times: Once for each character in the string until the end (null character).
As the string gets longer, the program reads and prints more characters, so the work grows with the string length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character reads and 10 prints |
| 100 | About 100 character reads and 100 prints |
| 1000 | About 1000 character reads and 1000 prints |
Pattern observation: The number of operations grows directly with the string length.
Time Complexity: O(n)
This means the time to read and print the string grows in a straight line with the string length.
[X] Wrong: "Reading or printing a string always takes the same time no matter how long it is."
[OK] Correct: The program must handle each character, so longer strings take more time.
Understanding how string input and output time grows helps you explain how programs handle user data efficiently.
"What if we used fgets() instead of scanf() to read the string? How would the time complexity change?"
Practice
Solution
Step 1: Understand string input functions
scanf with "%s" reads a single word string until whitespace.Step 2: Identify correct function for single word input
printf is for output, gets is unsafe, fgets reads whole line including spaces.Final Answer:
scanf("%s", str); -> Option DQuick Check:
Single word input = scanf("%s", str); [OK]
- Using printf instead of scanf for input
- Using fgets expecting single word input
- Using gets which is unsafe
str in C?Solution
Step 1: Identify the print format specifier for strings
"%s" is used in printf to print strings.Step 2: Match correct function and format
printf with "%d" is for integers, scanf is for input, gets reads input.Final Answer:
printf("%s", str); -> Option BQuick Check:
Print string = printf("%s", str); [OK]
- Using %d to print strings
- Using scanf instead of printf for output
- Using gets for printing
#include <stdio.h>
int main() {
char str[20];
scanf("%s", str);
printf("%s", str);
return 0;
}If the user inputs:
Hello WorldSolution
Step 1: Understand scanf with %s behavior
scanf("%s", str) reads input until first whitespace, so it reads only "Hello".Step 2: Output printed by printf
printf("%s", str) prints the stored string, which is "Hello".Final Answer:
Hello -> Option AQuick Check:
scanf with %s stops at space = Hello [OK]
- Expecting scanf %s to read full line with spaces
- Confusing scanf with fgets
- Assuming output includes 'World'
#include <stdio.h>
int main() {
char str[50];
scanf("%s", str);
printf("%s", str);
return 0;
}Solution
Step 1: Analyze scanf with %s for input
scanf("%s", str) stops reading input at first whitespace, so it cannot read full line with spaces.Step 2: Check other parts of code
printf("%s", str) correctly prints string, array size is sufficient, main has return type int.Final Answer:
scanf("%s", str) reads only until first space, not full line -> Option CQuick Check:
scanf %s stops at space, so not full line input [OK]
- Assuming scanf %s reads full line
- Thinking printf can't print strings
- Confusing array size issues
str of size 100 in C?Solution
Step 1: Understand functions for reading strings with spaces
scanf("%s", str) reads only until space, gets is unsafe and deprecated.Step 2: Identify safe function to read full line
fgets(str, 100, stdin) reads up to 99 characters including spaces safely.Final Answer:
fgets(str, 100, stdin); -> Option AQuick Check:
Use fgets for full line input with spaces [OK]
- Using scanf %s for full line input
- Using gets despite safety issues
- Confusing printf with input functions
Ready to prove it?
Pass the test and this pattern moves unknown
