Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    C++C++

    C++ fundamentals

    • Your first C++ program
    • Variables and types
    • Operators and input
    • Conditions: if and switch
    • Loops

    Functions and collections

    • Functions
    • References: by value or by reference?
    • Strings and arrays
    • std::vector
    • map, set and unordered_map

    Memory and pointers

    • Pointers
    • Stack, heap, new and delete
    • Smart pointers and RAII

    Classes and OOP

    • struct and class
    • Constructors and destructors
    • Inheritance and virtual functions
    • Templates

    The STL and professional C++

    • Algorithms and lambdas
    • Exceptions
    • Recursion
    • Project: a grade book
    C++

    Functions and collections · Lesson 8 of 21

    Strings and arrays

    std::string and its methods, fixed arrays, and std::array.

    9 minutes2 games

    std::string is text that grows and shrinks by itself, with many ready-made methods. Its characters are indexed from zero, just like arrays.

    C++
    #include <iostream>
    #include <string>
    
    int main() {
        std::string course = "SeaCode C++";
        std::cout << course.size() << " chars\n";
        std::cout << course[0] << " ... " << course.back() << "\n";
        std::cout << course.substr(0, 7) << "\n";          // from 0, take 7
    
        std::size_t pos = course.find("C++");
        if (pos != std::string::npos) std::cout << "found at " << pos << "\n";
    
        course += " track";
        course[0] = 's';
        std::cout << course << "\n";
        return 0;
    }

    course =

    S[0]
    e[1]
    a[2]
    C[3]
    o[4]
    d[5]
    e[6]
    [7]
    C[8]
    +[9]
    +[10]

    find("C++") found it at index 8.

    Important

    find returns std::string::npos when nothing is found, not -1. Always compare with it: npos is a huge size_t (unsigned) value, so comparing with -1 or < 0 misleads.

    Arrays

    C++
    #include <iostream>
    #include <array>
    
    int main() {
        int raw[4] = {90, 75, 88, 60};         // C-style array: fixed size
        std::array<int, 4> scores = {90, 75, 88, 60};
    
        int best = scores[0];
        for (std::size_t i = 1; i < scores.size(); i++) {
            if (scores[i] > best) best = scores[i];
        }
        std::cout << "best " << best << " of " << scores.size() << "\n";
        std::cout << "raw[2] = " << raw[2] << "\n";
        return 0;
    }

    A plain array (int raw[4]) forgets its size when passed to a function and never checks bounds. std::array knows its size via .size(), and .at(i) throws on a bad index instead of reading random memory. If the size changes, use std::vector (next lesson).

    What's the output?

    #include <iostream>
    #include <string>
    int main() {
        std::string s = "algorithm";
        std::cout << s.substr(2, 4) << s.size() << "\n";
    }

    What will this code print?

    Interview question

    How do you reverse a string, and check whether it's a palindrome?

    Exercise

    Count the vowels (a e i o u) in "programming is fun" and print the count.

    Expected output: 5

    C++
    #include <iostream>
    #include <string>
    
    int main() {
        std::string text = "programming is fun";
        int vowels = 0;
        
        std::cout << vowels << "\n";
        return 0;
    }

    Learn more from

    • cppreference: std::basic_stringen.cppreference.com
    • cppreference: std::arrayen.cppreference.com
    • learncpp: Introduction to std::stringlearncpp.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousReferences: by value or by reference?Nextstd::vector
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.