Cambo Freelance
HomeServicesArticlesTutorialsTeamCoursesContact
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

Send us a message

Ready to start your project? Get in touch with our team.

Contact Us

Services

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

Useful Links

  • Home
  • Services
  • Learning
  • About Us
  • Contact
  • Pricing

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Strings
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 11 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++BeginnerLesson 11 of 50CamboFreelanceJune 19, 2026

C++ Strings

Learn how to work with strings in C++: create, concatenate, and use essential string methods like find, substr, and replace.


Tutorials › C++ › Strings
Beginner8 min readLesson 11 of 50

In This Lesson

  • Creating Strings
  • Concatenation
  • String Methods
  • Accessing Characters
  • Quiz

Creating Strings

In C++, a string is an object of the std::string class. Include <string> to use it (often available via <iostream>).

#include <iostream>
#include <string>
using namespace std;

int main() {
    string greeting = "Hello, World!";
    cout << greeting << endl;
    cout << "Length: " << greeting.length() << endl;
    return 0;
}

Output:

Hello, World!
Length: 13

String Concatenation

Use the + operator to join strings:

string firstName = "John";
string lastName  = "Doe";
string fullName  = firstName + " " + lastName;
cout << fullName;   // John Doe

Useful String Methods

MethodDescriptionExample
.length()Returns the number of characterss.length()
.size()Same as length()s.size()
.toupper()Use with <algorithm>transform(...)
.find()Finds a substrings.find("lo")
.substr()Extracts a substrings.substr(0,5)
.erase()Removes characterss.erase(0,3)
.replace()Replaces a substrings.replace(0,5,"Hi")
#include <iostream>
#include <string>
using namespace std;

int main() {
    string s = "Hello, World!";
    cout << s.length()      << endl;    // 13
    cout << s.substr(7, 5)  << endl;    // World
    cout << s.find("World") << endl;    // 7

    s.replace(7, 5, "C++");
    cout << s << endl;                  // Hello, C++!
    return 0;
}

Accessing Characters

string s = "Hello";
cout << s[0];       // H
cout << s[4];       // o
cout << s.at(1);    // e  (safe, throws if out of range)

Exercise

Write a program that reads a string from the user and prints: its length, the first character, the last character, and the string reversed using a loop.

Show Solution
#include <iostream>
#include <string>
using namespace std;

int main() {
    string s;
    cout << "Enter a word: ";
    cin >> s;
    cout << "Length: " << s.length() << endl;
    cout << "First: "  << s[0] << endl;
    cout << "Last: "   << s[s.length()-1] << endl;
    cout << "Reversed: ";
    for (int i = s.length()-1; i >= 0; i--) cout << s[i];
    cout << endl;
    return 0;
}

Quiz

  1. Which method returns the number of characters in a C++ string?

    • A) count()
    • B) size()
    • C) chars()
    • D) len()
    Answer

    B) size() (also length() — both work identically).

  2. Which operator concatenates two strings?

    • A) &
    • B) *
    • C) +
    • D) .
    Answer

    C) +

Summary

  • string is a class from the <string> header.
  • Concatenate with +; access characters with [i] or .at(i).
  • Key methods: length(), substr(), find(), replace().
← Previous: Operators Next: C++ Math →
c++stringsstring-methodsbeginner
PreviousLesson 10: C++ OperatorsNextLesson 12: C++ Math
Back to All Tutorials