Course contentsShow
← Computer Science
Lesson 260 of 2,8726. Arrays and Basic CollectionsFree lesson

Strings as Arrays of Characters

Understanding strings as sequences of characters stored in contiguous memory, with indexing and length properties.

Strings as Arrays of Characters

What you'll learn: How strings are fundamentally sequences of individual characters stored side-by-side in memory, accessible by position.

The Core Idea

A string isn't a magical data type—it's actually an array of characters. When you write "Hello", the computer stores five characters ('H', 'e', 'l', 'l', 'o') in consecutive memory locations, just like an array of integers or any other data type. Each character occupies one slot, and you can access them using index positions starting from 0.

Think of a string like a train of boxcars, where each car holds exactly one letter. The train moves as a unit, but you can peek into any specific car by counting its position from the front.

Key Properties

Indexing: Just like arrays, you access individual characters by their position. In "Hello", index 0 gives you 'H', index 1 gives you 'e', and so on.

Length: Strings have a length property that tells you how many characters they contain—essentially the size of the underlying character array. "Hello" has length 5.

Contiguous Memory: The characters sit right next to each other in memory, making it efficient to read through them sequentially (like iterating through an array).

Iteration: You can loop through a string character-by-character using the same techniques you use for arrays—a for loop with an index counter, or iterating from start to end.

This underlying structure explains why many string operations mirror array operations: you can reverse strings, search for characters, count frequencies, and copy substrings—all leveraging the same principles you've learned with arrays.

Key Takeaway: Strings are simply arrays of characters stored in contiguous memory, supporting indexing, length queries, and iteration just like any other array type.