Case conversion functions in PHP - Time & Space Complexity
We want to understand how the time it takes to change text case grows as the text gets longer.
How does the program's work increase when we convert bigger strings?
Analyze the time complexity of the following code snippet.
$string = "Hello World!";
$lower = strtolower($string);
$upper = strtoupper($string);
// Convert each character to lowercase and uppercase
This code converts a string to all lowercase and all uppercase letters.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The functions check each character in the string one by one.
- How many times: Once for each character in the string.
As the string gets longer, the work grows in a straight line with the number of characters.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character checks |
| 100 | About 100 character checks |
| 1000 | About 1000 character checks |
Pattern observation: Doubling the string length roughly doubles the work.
Time Complexity: O(n)
This means the time to convert case grows directly with the string length.
[X] Wrong: "Changing case is instant no matter the string size."
[OK] Correct: Each character must be checked and changed, so longer strings take more time.
Understanding how simple string operations scale helps you explain efficiency clearly and confidently.
"What if we changed the string to an array of words and converted each word's case separately? How would the time complexity change?"