C++ 7
C++ 7
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 <iostream>
#include <string>
using namespace std;
int main() {
string greeting = "Hello";
cout << greeting;
return 0;
}
Run example »
String Concatenation
The + operator can be used between strings to add them together to make a
new string. This is called concatenation:
Example
#include <iostream>
#include <string>
using namespace std;
int main () {
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + lastName;
cout << fullName;
return 0;
}
Run example »
Note that we added a space after firstName to create a space between John and
Doe on output.
String Length
A string in C++ is actually an object, which contain functions that can perform
certain operations on strings. For example, the length of a string can be found
with the length() function:
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.length();
return 0;
}
Run example »
Access Strings
You can access the characters in a string by referring to its index number inside
square brackets [].
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string myString = "Hello";
cout << myString[0];
return 0;
}
Run example »
Note: String indexes start with 0: [0] is the first character. [1] is the second
character, etc.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string myString = "Hello";
cout << myString[1];
return 0;
}
Run example »
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string myString = "Hello";
myString[0] = 'J';
cout << myString;
return 0;
}
Run example »
Example
string firstName;
cout << "Type your first name: ";
cin >> firstName; // get user input from the keyboard
cout << "Your name is: " << firstName;
Example
string fullName;
cout << "Type your full name: ";
cin >> fullName;
cout << "Your name is: " << fullName;
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
#include <iostream>
#include <string>
using namespace std;
int main() {
string fullName;
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName;
return 0;
}
Run example »
Example
#include <iostream>
using namespace std;
int main () {
int x = 10;
int y = 20;
int z = x + y;
cout << z;
return 0;
}
Run example »
Example
#include <iostream>
#include <string>
using namespace std;
int main () {
string x = "10";
string y = "20";
string z = x + y;
cout << z;
return 0;
}
Run example »
Example
string x = "10";
int y = 20;
string z = x + y;
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 <iostream>
#include <string>
int main() {
std::string greeting = "Hello";
std::cout << greeting;
return 0;
}
Run example »
It is up to you if you want to include the standard namespace library or not.