Stringst Abdulmalik
Stringst Abdulmalik
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!
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
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