An array is a collection of elements of the same data type stored in contiguous memory locations.
Array index starts from 0
Example:
Stores multiple values using one variable
Easy access using index
Efficient memory usage
A string is a sequence of characters.
C++ supports strings in two ways:
C-style strings
C++ string class
Stored as an array of characters and end with '\0'.
std::string)Requires header file:
Example:
| Function | Description |
|---|---|
length() | Returns string length |
append() | Adds text |
compare() | Compares strings |
find() | Finds a character or word |
substr() | Extracts substring |
Example:
| Array | String |
|---|---|
| Stores same data type | Stores characters |
| Fixed size | Dynamic size |
| No built-in functions | Rich library support |
Arrays store multiple values of same type
Index starts from 0
Strings are easier to use with std::string
getline() reads full input with spaces
Arrays and strings are fundamental data structures in C++ used for storing and managing collections of data efficiently.
Take quizzes related to this topic and see where you stand!
Start Quiz Now