0% found this document useful (0 votes)
28 views3 pages

C Strings

Strings in C++ can store text. A string variable contains characters surrounded by double quotes. The length of a string can be found using the length() function. Strings can be concatenated using the + operator. Characters in a string can be accessed using indexes in square brackets. Special characters in strings require escaping with a backslash. User input to strings is better handled using getline() than the extraction operator due to whitespace handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

C Strings

Strings in C++ can store text. A string variable contains characters surrounded by double quotes. The length of a string can be found using the length() function. Strings can be concatenated using the + operator. Characters in a string can be accessed using indexes in square brackets. Special characters in strings require escaping with a backslash. User input to strings is better handled using getline() than the extraction operator due to whitespace handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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:

string greeting = "Hello";

String Concatenation

The + operator can be used between strings to add them together to make a
new string. This is called concatenation:

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

String Length

To get the length of a string, use the length() function:

Example
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.length();

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
string myString = "Hello";
cout << myString[0];
// Outputs H

Strings - Special Characters

Because strings must be written within quotes, C++ will misunderstand this
string, and generate an error:

string txt = "We are the so-called "Vikings" from the north.";

The solution to avoid this problem, is to use the backslash escape


character.

The backslash (\) escape character turns special characters into string
characters:

Escape character Result Description

\' ' Single quote

\" " Double quote

\\ \ Backslash

The sequence \" inserts a double quote in a string:

Example
string txt = "We are the so-called \"Vikings\" from the north.";

User Input Strings

It is possible to use the extraction operator >> on cin to display a


string entered by a user:
Example
string firstName;
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
string fullName;
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
string fullName;
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName;

// Type your full name: John Doe


// Your name is: John Doe

You might also like