Introduction to Strings
Explore the basics of strings as ordered sequences of characters in C++. Understand the difference between characters and strings, how strings are stored in memory like arrays, and the significance of null terminators and the std::string class. Learn key concepts such as string equality, empty strings, character encoding, and the distinction between digit characters and numeric values. This lesson sets the foundation for working with string algorithms and text manipulation in C++.
By this point, you are familiar with arrays as ordered collections of elements stored by position. Arrays provide a general way to store sequences of values such as integers, floating-point numbers, and objects. Each element in an array can be accessed efficiently using its index.
However, many important computational problems involve textual and symbolic data rather than only numeric values. Names, passwords, file paths, search queries, source code, and biological sequences are all examples of data that must be handled as sequences of characters. Although arrays can store characters, viewing such data only as a general array is often not enough for algorithmic study.
Character-based data gives rise to a different kind of problem. In numerical arrays, common tasks include searching for values, computing sums, or rearranging elements. In character sequences, the focus shifts to questions such as whether two words are equal, whether a pattern appears inside a larger text, how often a character occurs, whether a string is a palindrome, and how two strings compare.
Strings provide a natural way to represent this type of data. A string is an ordered sequence of characters, and it forms the basis of many important computational tasks such as text processing, pattern matching, and validation.
What is a character?
Before defining a string precisely, we need to be clear about what a string is made of. The basic building block of every string is a character.
A character is a single symbol. It can be a letter like 'a' or 'Z', a digit like '3' or '9', a space, a punctuation mark like '?' or '.', or a special symbol like '@' or '#'. Anything you can type on a keyboard is a character. Each character represents one unit of textual information, and it occupies exactly one position in memory.
It is important to distinguish a character from a string early on because they are two different things, even though they can look similar when written in code.
A single character is the smallest possible unit. A string is a collection of ...