capypad
0 day streak
cpp / beginner
Snippet

Text Manipulation with std::string

The std::string class lets you work with text easily. You can concatenate strings with +, get the length with .length(), and access individual characters by index.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>
int main() {
std::string name = "Alice";
std::string greeting = "Hello, " + name + "!";
std::cout << greeting << std::endl;
std::cout << "Length: " << greeting.length() << std::endl;
return 0;
}
Breakdown
1
std::string name = "Alice";
Declares a string variable with initial value
2
std::string greeting = "Hello, " + name + "!";
Concatenates multiple strings together
3
greeting.length()
Returns the number of characters in the string