Introduction to Strings
In C++, a string is a sequence of characters that is used to represent textual data. Strings are commonly used for storing and manipulating text in programs.
In C++, strings are defined using the std::string
class from the Standard Template Library (STL). To work with strings, you need to include the <string>
header.
Here's an example of declaring and printing a string in C++:
1#include <iostream>
2
3int main() {
4 std::string greeting = "Hello, world!";
5 std::cout << greeting << std::endl;
6 return 0;
7}
In this example, we declare a string variable greeting
and assign it the value "Hello, world!". We then use std::cout
to print the string to the console, followed by a newline character (std::endl
) to start a new line.
When you run this program, you will see the output:
1Hello, world!
This is a basic introduction to strings in C++. In the following lessons, we will explore more advanced concepts and operations related to strings.
xxxxxxxxxx
int main() {
std::string greeting = "Hello, world!";
std::cout << greeting << std::endl;
return 0;
}