Since string is used extensively, C++ provides a built-in string data type. Just like int, float or other data types, we can use string data type also. It simply makes using strings easier.
Same as cin and cout, string is also defined in the std namespace. To use strings in this way, we need to include the
#include <string>
We declare variables of type std::string as follows.
std::string name;
Here name is a string variable just like we have int variables, float variables or variables of other data types.
We assign value to a string variable just as we assign value to a variable of any other data type as follows.
name = "Hall";
We can also assign value to a string variable at the time of declaring it.
std::string name("Hall");
A string variable is just like any other variable.
Now let's see an example to print a string.
#include <iostream>
int main(){
std::string name;
name = "Hall";
std::cout << name << std::endl;
return 0;
}
Input a string
We already know how to input a string using a character array. Now, let's input a string using string variable.
We simply use cin to input a string as we used to do using character array.
#include <iostream>
using namespace std;
int main(){
string name;
cin >> name;
cout << name << endl;
return 0;
}
Hall
Now let's try giving a multi-word input.
#include <iostream>
using namespace std;
int main(){
string name;
cin >> name;
cout << name << endl;
return 0;
}
Hall
As in the above example, cin takes characters only up to the first whitespace. Thus the string variable name just stored the value Hall.
Taking multi-word input using std::getline
To take a multi-word string input, we use std::getline function. Let's see an example taking a multi-word input.
#include <iostream>
int main(){
std::string name;
std::getline(std::cin, name);
std::cout << name << std::endl;
return 0;
}
Hall Doe
std::getline(std::cin, name); - This is the syntax for using the getline() function. getline() function is also defined in the std namespace and thus we write it as std::getline.
getline() function takes two parameters. The first one is std::cin and the second one is the name of our string variable.
Let's append two strings
We can concatenate strings ( join strings ) using + operator and append one string to another using += operator.
#include <iostream>
using namespace std;
int main(){
string st1("Hello");
string st2("World");
cout << st1 + st2 << endl;
st1 += "cpp";
cout << st1 << endl;
return 0;
}
Hellocpp
In the above example, the string variables s1 and s2 stores the values "Hello" and "World" respectively.
cout << st1 + st2 << endl; - This is the same as writing cout << st1 << st2 << endl; which prints one string after the other.
st1 += "cpp"; - We added the string "cpp" to the value of the string variable st1 thus making its value "Hellocpp".
Member Functions
There are various member functions of std::string ( pre-defined functions which are defined in std::string ) like length(), size(), resize(), reserve(). Let's look at some of these.
length()
length() function is used to find the length of a string. This is a pre-defined function which belongs to std::string. Let's see an example for the same.
#include <iostream>
int main(){
std::string name;
name = "I have 4 chocolates";
std::cout << name.length() << std::endl;
return 0;
}
Thus length() function returned the length of the string 'name' including whitespace characters.
empty
empty() function returns whether a string is empty or not. It returns 1 if the string length is 0 and 0 if not.
#include <iostream>
#include <string>
int main(){
std::string s1, s2;
s1 = "";
s2 = "Brownie";
std::cout << s1.empty() << std::endl;
std::cout << s2.empty() << std::endl;
return 0;
}
0
Since s1 stored a null string, so s1.empty() returned 1 and since s2 stored the string "Brownie", s2.empty() returned 0
size()
It is also used to return the length of the string. This returns the same value as that by length().
#include <iostream>
#include <string>
int main(){
std::string name;
name = "I have 4 chocolates";
std::cout << name.size() << std::endl;
return 0;
}
resize()
resize() function resizes our string to a specified length.
Suppose, the specified length is 5. If the specified length (i.e. 5 ) is smaller than the current length ( suppose 7 ), then the string will contain only the first 5 characters. If the specified length ( 5 ) is greater than the current length ( suppose 3 ), then the string will contain 5 characters ( 3 of the original string and two other specified characters ). Let's see an example to understand this.
#include <iostream>
#include <string>
int main()
{
std::string s = "I love C";
int size = s.size(); // size of s
s.resize( size+2, '+' ); // resizing s
std::cout << s << std::endl;
return 0;
}
In this example, we first stored the size ( 8 ) of the original value of the string s in a variable size.
s.resize( size+2, '+' ); - This statement resized the string 's' to 'size + 2' i.e. 10 and assigned the character ' + ' to the two extra character places in the resized string.
clear()
This function clears the content of strings.
#include <iostream>
#include <string>
int main()
{
std::string s = "I love C";
s.clear();
std::cout << "Value of s is :" << s << std::endl;
return 0;
}
operator[ ]
It returns the character at some specified position in a string. Let's see an example.
#include <iostream>
#include <string>
int main()
{
std::string s = "I love C";
std::cout << "Character at fifth position : " << s[5] << std::endl;
return 0;
}
Here, s represents the whole string and s[i] represents a character in a string at the ith position. Note that the position of the first character in a string is 0.
at()
It also returns the character at some specified position in a string.
#include <iostream>
#include <string>
int main()
{
std::string s = "I love C";
std::cout << "Character at fifth position : " << s.at(5) << std::endl;
return 0;
}
compare()
compare() function compares the value of a string ( str1 ) with another string ( str2 ).
It returns 0 if both the strings are equal.
It returns a positive value if either str1 > str2 or the first unmatched letter of str1 is greater than that of str2.
It returns a negative value if either str1 < str2 or the first unmatched letter of str1 is less than that of str2.
#include <iostream>
#include <string>
int main()
{
std::string str1 ("have apple");
std::string str2 ("have app");
std::cout << str1.compare(str2) << std::endl;
return 0;
}
Here, the length of string 'str1' is 2 more than that of string 'str2'. Therefore, the output is 2. If the values of 'str1' and 'str2' are interchanged, then the output will be -2.
Let's see one more example.
#include <iostream>
#include <string>
int main()
{
std::string str1 ("have gpple");
std::string str2 ("have apple");
std::cout << str1.compare(str2) << std::endl;
return 0;
}
This time, the first unmatched character in 'str1' and 'str2' are 'g' and 'a' respectively. Since the ASCII value of 'g' is greater than that of 'a', we got a positive output. On interchanging the values of the two strings, we will get a negative answer.
find()
find() function finds the position of the first occurrence of a character or string in a string.
#include <iostream>
#include <string>
int main()
{
std::string str1 ("I love C++");
std::cout << str1.find('l') << std::endl;
return 0;
}
In this example, find() returned the position of 'l' in the string str1. Now, let's see an example to find the position of a string in another string.
#include <iostream>
#include <string>
int main()
{
std::string str1 ("I love C++");
std::string str2 ("C++");
std::cout << str1.find(str2) << std::endl;
return 0;
}
substr()
substr() function returns a substring from a string by specifying its position.
#include <iostream>
#include <string>
int main()
{
std::string str1 ("I love C++");
std::cout << str1.substr(3) << std::endl;
return 0;
}
The above example printed all the characters from position 3 till the end of the string str1.
#include <iostream>
#include <string>
int main()
{
std::string str1 ("I love C++");
std::cout << str1.substr(3,5) << std::endl;
return 0;
}
Here, 5 characters starting from position 3 got printed.
How difficult life may seem, there is always something you can do and succeed at.
-Stephen Hawking