⬅ Previous Next ➡

Arrays & Strings

Arrays & Strings in C++
  • Arrays store multiple values of the same type in contiguous memory.
  • C++ supports 1D arrays, 2D arrays, and array of objects.
  • C-style strings are character arrays ending with \0.
  • std::string is a modern C++ class for easier and safer string handling.

1) One-Dimensional Array (1D)

  • Syntax: type arr[size];
  • Index starts from 0 to size-1.
#include <iostream>
using namespace std;

int main() {
    int a[5] = {10, 20, 30, 40, 50};

    for (int i = 0; i < 5; i++) {
        cout << a[i] << " ";
    }
    cout << endl;

    return 0;
}

2) Two-Dimensional Array (2D)

  • 2D array is like a matrix (rows × columns).
  • Syntax: type arr[rows][cols];
#include <iostream>
using namespace std;

int main() {
    int m[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            cout << m[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

3) Array of Objects

  • We can create arrays of class objects to store multiple object records.
#include <iostream>
using namespace std;

class Student {
public:
    int id;

    void input() {
        cin >> id;
    }

    void display() {
        cout << "ID: " << id << endl;
    }
};

int main() {
    Student s[3];

    cout << "Enter 3 student ids: ";
    for (int i = 0; i < 3; i++) {
        s[i].input();
    }

    for (int i = 0; i < 3; i++) {
        s[i].display();
    }

    return 0;
}

4) C-style Strings (char array)

  • C-style string ends with \0 (null character).
  • Declaration: char name[] = "Sourav";
  • Functions (from <cstring>): strlen, strcpy, strcat, strcmp.
#include <iostream>
#include <cstring>
using namespace std;

int main() {
    char s1[20] = "Hello";
    char s2[20] = "World";

    cout << "Length of s1: " << strlen(s1) << endl;

    strcat(s1, " ");
    strcat(s1, s2);

    cout << "After concat: " << s1 << endl;

    return 0;
}

5) Reading C-style Strings (cin.getline)

  • cin stops at space, so use cin.getline() for full line input.
#include <iostream>
using namespace std;

int main() {
    char name[50];

    cout << "Enter full name: ";
    cin.getline(name, 50);

    cout << "Name: " << name << endl;
    return 0;
}

6) std::string Class

  • std::string is safer than char arrays (auto memory management).
  • Common methods: length(), size(), append(), substr(), find().
#include <iostream>
#include <string>
using namespace std;

int main() {
    string a = "Hello";
    string b = "C++";

    cout << "Length: " << a.length() << endl;

    string c = a + " " + b;
    cout << "Joined: " << c << endl;

    cout << "Substr: " << c.substr(0, 5) << endl;

    return 0;
}

7) Compare C-style vs std::string

  • C-style: char array + \0, needs <cstring> functions, risk of overflow.
  • std::string: easy operations, dynamic size, safer and recommended.

8) Quick Notes

  • 1D array stores elements in a single line, 2D array stores in rows/columns.
  • Array of objects stores multiple object records.
  • C-style strings end with \0 and use <cstring> functions.
  • std::string is modern, safer, and easier to use.
⬅ Previous Next ➡