UNIT1 :
Introduction to C++ Strings
Adjectives of this unit
- C++ Strings .
- String Concatenation.
- C++ Numbers and Strings.
- C++ String Length.
- C++ Access Strings.
- Change String Characters.
- User Input Strings.
T.SUB: Abdulmalik A Alsarori
C++ Strings
One of the most useful data types supplied in the C++
libraries is the string.
A string is a variable that stores a sequence of letters or
other characters, such as "Hello" or
“Weicome!".
To use strings, you must include an additional header file
in the source code, the <string> library.
#include <string> // Include the string library
string greeting = "Hello"; // Create a string variable
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;
output
JohnDoe
String Concatenation
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 firstName = "John ";
string lastName = "Doe";
string fullName = firstName.append(lastName);
cout << fullName;
output
JohnDoe
C++ Numbers and Strings
Adding Numbers and Strings
WARNING!
C++ uses the + operator for both addition and concatenation.
Numbers are added. Strings are concatenated.
Example 1 Example2 Example3
int x = 200; string x = "200"; int x = 200;
int y = 20; string y = "20"; string y = "20";
int z = x + y; string z = x + y; string z = x + y;
output output output
220 20020 error
C++ String Length
String Length: to get the length of a string, use
the length() function:
Also we can use another size() function to get
the length of any string
Example
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.length();
cout << "The size of the txt string is: " << txt.size();
output
The length of the txt string is:26
The size of the txt string is:26
C++ Access Strings
You can access the characters in a string by
referring to its index number inside square
brackets [].
Example
string myString = "WEICOME ";
cout << "myString[0] " << myString[0]<<endl;
cout << "myString[5] " << myString[5];
output
myString[0] W
myString[5] M
Change String Characters
To change the value of a specific character in a
string, refer to the index number, and use single
quotes:
Example
string myString = " HEICOMY";
myString[0] = ’W';
myString[6] = ’E';
cout << myString;
output
WEICOME
C++ 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:\n ";
cin >> firstName; // get user input from the keyboard
cout << " \nYour name is : " << firstName;
output
Type your first name :
Ahmed
Your name is : Ahmed
User Input Strings
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 firstName;
cout << "Type your first name:\n ";
cin >> firstName; // get user input from the keyboard
cout << “\nYour name is : " << firstName;
output
Type your first name:
Ahmed Ali
Your name is : Ahmed
From the example above, you would expect the program to print
“Ahmed Ali", but it only prints “Ahmed".
User Input Strings
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;
output
Type your first name:
Ahmed Ali
Your name is : Ahmed Ali