Arrays and Strings in C++
📘 C++
👁 30 views
📅 Dec 22, 2025
⏱ Estimated reading time: 2 min
An array is a collection of elements of the same data type stored in contiguous memory locations.
Declaration of Array
Example
Initialization of Array
Accessing Array Elements
Array index starts from 0
Traversing an Array
Types of Arrays
1. One-Dimensional Array
2. Two-Dimensional Array
Example:
Advantages of Arrays
-
Stores multiple values using one variable
-
Easy access using index
-
Efficient memory usage
Strings in C++
A string is a sequence of characters.
C++ supports strings in two ways:
-
C-style strings
-
C++ string class
1. C-Style Strings
Stored as an array of characters and end with '\0'.
2. String Class (std::string)
Requires header file:
Example:
String Input and Output
Common String Functions
| Function | Description |
|---|---|
length() | Returns string length |
append() | Adds text |
compare() | Compares strings |
find() | Finds a character or word |
substr() | Extracts substring |
Example:
Difference Between Array and String
| Array | String |
|---|---|
| Stores same data type | Stores characters |
| Fixed size | Dynamic size |
| No built-in functions | Rich library support |
Key Points
-
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
Conclusion
Arrays and strings are fundamental data structures in C++ used for storing and managing collections of data efficiently.
🔒 Some advanced sections are available for Registered Members
Register Now
Register Now
Share this Post
← Back to Tutorials