Character
Overview
A Character data type represents a single textual symbol. It can be a letter, digit, punctuation mark, whitespace, or even an emoji, depending on the character encoding. Think of it as the atom of text, small on its own, powerful in swarms.
What Is a Character?
A character is not the same as a string.
- Character → one symbol (
'A','7','?') - String → sequence of characters (
"Hello")
Under the hood, characters are stored as numeric codes defined by an encoding standard.
Common Character Encodings
| Encoding | Description |
|---|---|
| ASCII | 7-bit encoding, 128 characters |
| Extended ASCII | 8-bit, 256 characters |
| Unicode | Universal standard |
| UTF-8 | Variable-length Unicode encoding |
| UTF-16 | Fixed/variable-length Unicode encoding |
Modern systems almost always use Unicode, allowing text from every writing system to coexist peacefully 🌍.
How It Works
When you write:
'A'
The system stores:
- A numeric code (example:
65for'A'in ASCII) - Interprets it according to the chosen encoding
Operations on characters usually involve:
- Comparison
- Case conversion
- Classification (letter, digit, symbol)
Common Operations
| Operation | Example |
|---|---|
| Comparison | 'a' < 'b' |
| Case conversion | 'a' → 'A' |
| Classification | isDigit('5') |
| Encoding conversion | UTF-8 ↔ UTF-16 |
Example
Pseudocode
ch = 'K'
if isUpperCase(ch):
print("Uppercase letter")
Real-world analogy
A character is like a single tile in a Scrabble set. Alone it has meaning, together they form words, sentences, and entire stories 🧩.
Time and Space Complexity
- Space: O(1)
- Operations: O(1)
Even with Unicode, individual character handling stays constant-time.
Use Cases
- Text processing
- Parsing input
- Compilers and interpreters
- File encoding and decoding
- Natural language processing
Advantages
- Precise control over text
- Efficient storage for single symbols
- Essential building block for strings
Limitations
- Cannot represent text sequences alone
- Unicode handling can be tricky across encodings