Skip to main content
Data Structures
CHAPTER 05 Beginner

Strings and Character Arrays

Updated: May 17, 2026
15 min read

# 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 of char 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.

c
123456789101112
// C Example: A String is a Character Array
#include <stdio.h>

int main() {
    // Array of 6 characters: H, e, l, l, o, \0
    char greeting[6] = {&#039;H&#039;, &#039;e&#039;, &#039;l&#039;, &#039;l&#039;, &#039;o&#039;, &#039;\0&#039;};
    
    // The CPU prints letters until it hits the '\0'
    printf("%s", greeting); 
    
    return 0;
}

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.

java
123456789101112131415161718
// Java Example: Reversing a String Array In-Place
public class StringAlgorithms {
    public static void reverseString(char[] str) {
        int left = 0;
        int right = str.length - 1;
        
        while (left < right) {
            // Swap the characters
            char temp = str[left];
            str[left] = str[right];
            str[right] = temp;
            
            // Move pointers inward
            left++;
            right--;
        }
    }
}

#### 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").

python
123456789101112
# Python Example: Palindrome Check
def is_palindrome(word):
    left = 0
    right = len(word) - 1
    
    while left < right:
        if word[left] != word[right]:
            return False # Mismatch found!
        left += 1
        right -= 1
        
    return True # Clean sweep, it's a palindrome

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.
cpp
12345678910111213141516171819
// C++ Example: Convert to Lowercase via Math
#include <iostream>
using namespace std;

void toLowercase(string &str) {
    for(int i = 0; i < str.length(); i++) {
        // If character is between 'A' and 'Z'
        if(str[i] >= &#039;A&#039; && str[i] <= &#039;Z&#039;) {
            str[i] = str[i] + 32; // Mathematical shift to lowercase!
        }
    }
}

int main() {
    string word = "HeLLo";
    toLowercase(word);
    cout << word; // Outputs "hello"
    return 0;
}

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. 1. Write a function to check if two strings are "Anagrams" (contain the exact same letters, just scrambled).
  1. 2. Using ASCII math, write a function to convert a lowercase string back to uppercase.

11. MCQs with Answers

Question 1

In computer memory, how does the CPU fundamentally store and process alphabetical characters like 'A' or 'B'?

Question 2

In the C programming language, what is the architectural purpose of the Null Terminator (\0)?

Question 3

What does it mean when a language dictates that String objects are "Immutable"?

Question 4

Because of String Immutability in Java, concatenating strings iteratively inside a massive 10,000-iteration for loop is considered a disastrous anti-pattern. Why?

Question 5

When utilizing the Two-Pointer algorithm to reverse a character array in-place, what is the Space Complexity?

Question 6

When determining if a string is a Palindrome using Two Pointers, what is the Time Complexity?

Question 7

In the ASCII table, the integer value for the uppercase letter 'A' is 65. What is the value for 'C'?

Question 8

How does the ASCII mathematical shift logic convert an uppercase letter to a lowercase letter?

Question 9

If a technical interviewer asks you to manipulate a string, what underlying data structure should you mentally visualize?

Question 10

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 StringBuilder class overcome the performance bottleneck of iterative string concatenation?"

Common Pitfalls:

  • Using .substring() or string slicing str[1:5] inside loops during interviews. Slicing forces an O(n) string copy operation. Use index pointers instead!

13. FAQs

Q: What is the difference between ASCII and Unicode? A: ASCII is old and only holds 128 characters (English letters, numbers, punctuation). Unicode (UTF-8) is massive. It holds every character of every language on Earth, plus Emojis! (Yes, emojis are just integers translated by Unicode).

14. Summary

A String is an illusion. By understanding that strings are fundamentally standard Character Arrays, we unlock the ability to manipulate text using basic index math and Two-Pointer logic. Recognizing the memory overhead of Immutability is the hallmark of a Senior Engineer.

15. Next Chapter Recommendation

We have mastered iterative loops to traverse arrays and strings. However, some problems cannot be solved with simple loops. What if a function could call *itself* to solve a puzzle? In Chapter 6: Recursion Basics, we will break our brains learning the most powerful algorithmic technique in computer science.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·