- C++ Introduction
- C++ Getting Started
- C++ Syntax
- C++ Output (Print Text)
- C++ Comments
- C++ Variables
- C++ Declare Multiple Variables
- C++ Identifiers
- C++ User Input
- C++ Data Types
- C++ Operators
- C++ Strings
- C++ Math
- C++ Booleans
- C++ Conditions
- C++ Switch
- C++ While Loop
- C++ For Loop
- C++ Break And Continue
- C++ Arrays
- C++ References
- C++ Pointers
- C++ Functions
- C++ Function Overloading
- C++ OOP
- C++ Classes And Objects
- C++ Class Methods
- C++ Constructors
- C++ Access Specifiers
- C++ Encapsulation
- C++ Inheritance
- C++ Multilevel Inheritance
- C++ Multiple Inheritance
- C++ Inheritance Access
- C++ Polymorphism
- C++ Files
- C++ Exceptions
- C++ How To Add Two Numbers
C++ Strings
C++ Strings
Strings are used for storing text.
A string
variable contains a collection of characters surrounded by double quotes:
Example
Create a variable of type string
and assign it a value:
To use strings, you must include an additional header file in the source code, the <string>
library:
Example
#include <string>
// Create a string variable
string greeting = "Hello";
Practice Excercise Practice now
C++ String Concatenation
The +
operator can be used between strings to add them together to make a new string. This is called concatenation:
Example
string lastName = "Doe";
string fullName = firstName + lastName;
cout << fullName;
In the example above, we added a space after firstName to create a space between John and Doe on output. However, you could also add a space with quotes (
" "
or ' '
):
Example
string lastName = "Doe";
string fullName = firstName + " " + lastName;
cout << fullName;
Append
A string in C++ is actually an object, which contain functions that can perform certain operations on strings. For example, you can also concatenate strings with the append()
function:
Example
string lastName = "Doe";
string fullName = firstName.append(lastName);
cout << fullName;
Practice Excercise Practice now
C++ Numbers And Strings
Adding Numbers and Strings
WARNING!
C++ uses the +
operator for both addition and concatenation.
Numbers are added. Strings are concatenated.
If you add two numbers, the result will be a number:
Example
int y = 20;
int z = x + y; // z will be 30 (an integer)
If you add two strings, the result will be a string concatenation:
Example
string y = "20";
string z = x + y; // z will be 1020 (a string)
If you try to add a number to a string, an error occurs:
Example
int y = 20;
string z = x + y;
Practice Excercise Practice now
C++ String Length
To get the length of a string, use the length()
function:
Example
cout << "The length of the txt string is: " << txt.length();
Tip: You might see some C++ programs that use the
size()
function to get the length of a string. This is just an alias of length()
. It is completely up to you if you want to use length()
or size()
:
Example
cout << "The length of the txt string is: " << txt.size();
Practice Excercise Practice now
C++ Access Strings
You can access the characters in a string by referring to its index number inside square brackets []
.
This example prints the first character in myString:
Example
cout << myString[0];
// Outputs H
This example prints the second character in myString:
Example
cout << myString[1];
// Outputs e
Change String Characters
To change the value of a specific character in a string, refer to the index number, and use single quotes:
Example
myString[0] = 'J';
cout << myString;
// Outputs Jello instead of Hello
Practice Excercise Practice now
C++ User Input Strings
It is possible to use the extraction operator >>
on cin
to display a string entered by a user:
Example
cout << "Type your first name: ";
cin >> firstName; // get user input from the keyboard
cout << "Your name is: " << firstName;
// Type your first name: John
// Your name is: John
However, cin
considers a space (whitespace, tabs, etc) as a terminating character, which means that it can only display a single word (even if you type many words):
Example
cout << "Type your full name: ";
cin >> fullName;
cout << "Your name is: " << fullName;
// Type your full name: John Doe
// Your name is: John
From the example above, you would expect the program to print "John Doe", but it only prints "John".
That's why, when working with strings, we often use the getline()
function to read a line of text. It takes cin
as the first parameter, and the string variable as second:
Example
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName;
// Type your full name: John Doe
// Your name is: John Doe
Practice Excercise Practice now
C++ String Namespace
Omitting Namespace
You might see some C++ programs that runs without the standard namespace library. The using namespace std
line can be omitted and replaced with the std
keyword, followed by the ::
operator for string
(and cout
) objects:
Example
#include <string>
int main() {
std::string greeting = "Hello";
std::cout << greeting;
return 0;
}
Practice Excercise Practice now
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP