0
0
Data Structures Theoryknowledge~6 mins

String as character array in Data Structures Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you want to store a word or sentence in a way that a computer can understand and work with easily. One simple way is to think of a string as a list of individual letters, each stored in order. This helps computers handle text by breaking it down into smaller, manageable pieces.
Explanation
Character Storage
A string is stored as a sequence of characters, each occupying a position in an array. This means each letter or symbol is kept in a specific order, allowing easy access to any character by its position number.
Strings are stored as ordered collections of characters in an array.
Indexing
Each character in the string has an index, usually starting from zero. This index helps locate and retrieve characters quickly without scanning the entire string.
Character positions in a string are accessed using zero-based indexes.
Mutability
Depending on the programming language, strings as character arrays can be mutable or immutable. Mutable means you can change characters directly, while immutable means you cannot change the string once created.
Strings may or may not allow direct changes to their characters depending on the language.
Null Terminator
In some languages like C, strings end with a special character called the null terminator to mark where the string finishes. This helps the computer know the string's length without storing it separately.
A null terminator marks the end of a string in certain languages.
Real World Analogy

Think of a string like a row of mailboxes, each holding one letter. You can find any letter by looking at its mailbox number. Sometimes, a special mailbox at the end tells you there are no more letters.

Character Storage → Each mailbox holding one letter in a row
Indexing → Mailbox numbers starting from zero to find letters
Mutability → Whether you can open a mailbox and change the letter inside
Null Terminator → A special mailbox at the end signaling no more letters
Diagram
Diagram
┌─────┬─────┬─────┬─────┬─────┐
│ H   │ e   │ l   │ l   │ o   │
├─────┼─────┼─────┼─────┼─────┤
│ 01234   │
└─────┴─────┴─────┴─────┴─────┘
          ↓
      Null terminator (\0)
This diagram shows a string as an array of characters with their indexes and a null terminator marking the end.
Key Facts
StringA sequence of characters stored in order.
Character ArrayAn array where each element holds one character of a string.
IndexA number representing the position of a character in a string, starting at zero.
Null TerminatorA special character marking the end of a string in some languages.
Mutable StringA string whose characters can be changed after creation.
Immutable StringA string that cannot be changed once created.
Code Example
Data Structures Theory
string = ['H', 'e', 'l', 'l', 'o', '\0']

# Accessing characters by index
print(string[0])  # H
print(string[4])  # o

# Finding the end of the string
index = 0
while string[index] != '\0':
    index += 1
print(f"Length of string is {index}")
OutputSuccess
Common Confusions
Thinking strings are always mutable arrays of characters.
Thinking strings are always mutable arrays of characters. Some languages use immutable strings, so you cannot change characters directly after creation.
Believing the string length is always stored separately.
Believing the string length is always stored separately. In some languages, strings end with a null terminator instead of storing length explicitly.
Assuming indexing starts at 1 instead of 0.
Assuming indexing starts at 1 instead of 0. Most programming languages use zero-based indexing for strings.
Summary
Strings can be thought of as arrays where each character has a specific position.
Indexes start at zero, making it easy to find and use characters in a string.
Some languages use a null terminator to mark the end of a string instead of storing its length.