Strings and Character Arrays
# CHAPTER 5
Strings and Character Arrays
1. Introduction
When you type your name into a website, you are interacting with a String. But computers do not understand the alphabet. A CPU only understands binary math (0s and 1s). How does a CPU know the difference between the letter 'A' and the letter 'Z'? In this chapter, we will learn that a String is not magic. Under the hood of almost every programming language, a String is simply a primitive Array of Characters, translated mathematically using ASCII or Unicode encoding.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand how Characters are mapped to Integers in RAM.
- Differentiate between a Character Array and a String Object.
-
Understand the Null-Terminator (
\0) in C/C++.
- Perform essential algorithmic string manipulations (Reversal, Palindrome checking).
3. Characters as Numbers (ASCII)
The computer maps every letter, number, and symbol to a specific integer. The classic standard is ASCII.- 'A' = 65, 'B' = 66, 'C' = 67 ...
- 'a' = 97, 'b' = 98, 'c' = 99 ...
- '0' (the character zero) = 48
When you write char letter = 'A';, the computer actually stores the integer 65 in memory. This means you can do math on letters!
'A' + 1 = 'B' (65 + 1 = 66).
4. The Anatomy of a String (The C Way)
In low-level languages like C, the "String" data type does not exist! A string is quite literally an Array ofchar types.
Because an array is just a block of memory, the computer needs to know where the word ends. C uses a special, hidden character called the Null Terminator (\0) to mark the end of the text.
5. String Objects (Java, Python, C++)
In modern languages, Strings are highly complex Objects that wrap the character array in powerful methods.Immutability:
In Java and Python, Strings are Immutable. This means once a String is created in memory, its text can never, ever be changed.
If you have a string s = "Hello" and you do s = s + " World", the CPU does *not* modify the original memory. It creates a brand new, completely separate string "Hello World" somewhere else in RAM and updates the pointer.
*Warning:* Doing massive string concatenations inside a for loop in Java or Python will rapidly exhaust your RAM and crash the program because it creates millions of abandoned string objects. (Use StringBuilder in Java to fix this!).
6. Core String Algorithms
#### 6.1 String Reversal (O(n) Time, O(1) Space) How do you reverse the word "HELLO" to "OLLEH" efficiently? You use the Two-Pointer Technique.
#### 6.2 Checking for a Palindrome (O(n) Time) A palindrome is a word that is spelled the same forwards and backwards (e.g., "RADAR", "RACECAR").
7. Mini Project: Toggling Letter Case
Because characters are just numbers, we can convert an uppercase string to a lowercase string using pure mathematics! In ASCII, the difference between 'A' (65) and 'a' (97) is exactly 32.8. Common Mistakes
-
Assuming Strings are primitives: In Java and Python,
==sometimes checks if two strings are the *exact same object in physical RAM*, not if they contain the same letters. Always use.equals()in Java to compare the actual text.
- Ignoring Substring Costs: Taking a substring (e.g., slicing a string) is an O(n) operation in modern languages, because it requires allocating a brand new character array and copying the letters over!
9. Best Practices
- Use Two Pointers: Almost all string-based coding interview questions (Palindromes, Reversals, Anagrams) can be solved optimally using the Two-Pointer technique shown above.
10. Exercises
- 1. Write a function to check if two strings are "Anagrams" (contain the exact same letters, just scrambled).
- 2. Using ASCII math, write a function to convert a lowercase string back to uppercase.
11. MCQs with Answers
In computer memory, how does the CPU fundamentally store and process alphabetical characters like 'A' or 'B'?
In the C programming language, what is the architectural purpose of the Null Terminator (\0)?
What does it mean when a language dictates that String objects are "Immutable"?
Because of String Immutability in Java, concatenating strings iteratively inside a massive 10,000-iteration for loop is considered a disastrous anti-pattern. Why?
When utilizing the Two-Pointer algorithm to reverse a character array in-place, what is the Space Complexity?
When determining if a string is a Palindrome using Two Pointers, what is the Time Complexity?
In the ASCII table, the integer value for the uppercase letter 'A' is 65. What is the value for 'C'?
How does the ASCII mathematical shift logic convert an uppercase letter to a lowercase letter?
If a technical interviewer asks you to manipulate a string, what underlying data structure should you mentally visualize?
Why is calculating the length of a string O(n) in C, but O(1) in Java?
12. Interview Preparation
Top Interview Questions:- *Algorithmic Pattern:* "Write a function that takes a string and returns the first non-repeating character in O(n) time." *(Hint: Use an Array of size 26, or a Hash Map, to count character frequencies).*
-
*Core Mechanics:* "Explain the concept of String Immutability in Python/Java. Why did the language designers choose to make strings immutable? How does a
StringBuilderclass overcome the performance bottleneck of iterative string concatenation?"
Common Pitfalls:
-
Using
.substring()or string slicingstr[1:5]inside loops during interviews. Slicing forces an O(n) string copy operation. Use index pointers instead!