Introducing String Data Type
Learn to declare, initialize, and handle the strings.
The string
data type
C++ is built on C, and it provides a data type that is defined in the namespace std
and requires string
as a header file.
C++ simplifies string manipulation in several ways. It even simplifies the declaration of a string over cstring
so that there is no need to declare it as an array (it abstracts out that detail inside it).
Let’s practice how to use the string
as a data type.
string
variable: Declaration and printing
Here’s the code for declaring several kinds of simple initialization:
string a = {'C','+','+'};string b = "C++";cout << "Before Updating..."<<endl;cout << "a: "<<a<<endl; // We will discuss below how this print is working.cout << "b: "<<b<<endl;a[0] = 'A'; // a string variable allows array like accessb[0] = 'B';cout << "\nAfter Updating..."<<endl;cout << "a: "<<a<<endl; // it displays exactly like any other variablecout << "b: "<<b<<endl;
Printing the string is like printing the variable, it gives the effect of just like a simple variable of any primitive data type (int
, char
, double
, etc.).
Traversing in string
The string
variable can easily be traversed through just like an array. Here’s a sample example:
string a = "abcd";cout << "\nstring traversal..."<<endl;for(int i=0; i<a.size(); i++){cout << a[i] ;if(i!=a.size()-1)cout<<" . ";}cout << endl;
The dot operator (
.
) in C++ is used to access the members (where a member could be another variable or a function) of an. For example, to use the function object a non-primitive variable that in itself contains primitive or non-primitive variables or functions getline()
withcin
, we use the dot operator just as we use the dot operator to use thesize()
operator.
Reassigning a string
The strings
can be re-assigned without any loop of copying:
string c = "Destroy";string d = "Earth";cout << "\nInitial Msg"<<endl;cout << "c = "<<c<<endl;cout << "d = "<<d<<endl;cout << "\nAfter re-assignment"<<endl;c = "Save"; // "Destroy" will be replaced with "Save"cout << "c = "<<c<<endl;cout << "d = "<<d<<endl;
Concatenating the strings
Concatenating is very easy, you can even expand or shrink the array, it does automatically over reassignment:
// string concatenation is just like addition of variablescout << "\nConcatenation"<<endl;string sp = " ";string msg = c+sp+d;cout << msg<<endl;
Instruction: Add the following code in the above playground.
Comparison of the strings
Comparing the strings is relatively easy. The character stored inside the strings is compared based on lexicographic order (the word which appears in the English dictionary first is considered lower in rank as compared to the word which appears later). Here are a few examples of these comparisons:
#include<iostream>#include<string>using namespace std;int main(){// Comparison is just like comparing two numberscout << "\nComparing the strings...!"<<endl;string b = "abc";string c = "def";cout << "\n1. \n";cout <<b<<"<"<<c<<": "<<(b<c)<<endl;cout <<b<<">"<<c<<": "<<(b>c)<<endl;cout <<b<<"=="<<c<<": "<<(b==c)<<endl;b = "def";c = "abc";cout << "\n2. \n";cout <<b<<"<"<<c<<": "<<(b<c)<<endl;cout <<b<<">"<<c<<": "<<(b>c)<<endl;cout <<b<<"=="<<c<<": "<<(b==c)<<endl;b = "abc";c = "abc";cout << "\n3. \n";cout <<b<<"<"<<c<<": "<<(b<c)<<endl;cout <<b<<">"<<c<<": "<<(b>c)<<endl;cout <<b<<"=="<<c<<": "<<(b==c)<<endl;b = "abc";c = "ABC";// the comparison works based on ASCII, and remember int('A')=65 and int('a')=97cout << "\n4. \n";cout <<b<<"<"<<c<<": "<<(b<c)<<endl;cout <<b<<">"<<c<<": "<<(b>c)<<endl;cout <<b<<"=="<<c<<": "<<(b==c)<<endl;}
Taking string from the console or file stream
We can easily take the input from the console or from a file, just like taking any variable.
Reading from the console through cin>>
(or ifstream Rdr
through Rdr>>
) will ignore all the spaces in the beginning and then keep reading characters one by one and concatenate them inside a variable until it finds a delimiter character such as {' ', '\t', '\n', '\r'}
.
Look at the code snippet below, where we take two inputs from the user through cin
and store them inside c
and d
and display them:
string c, d;// Taking input from the console or filecin>>c;cin>>d;cout << "c: "<< c << endl<<" d: "<< d << endl;
Reading through the getline()
function
We can use the getline() function to read a string with a delimiter character as input.
Here’s the testing program:
#include <iostream>#include <string>using namespace std;int main() {// your code goes herestring testing;getline(cin, testing); // Input a line "String is great"cout << "reading through getline: " << testing << endl;// Using delimetergetline(cin, testing, '.'); // Input a line "String is great. And awesome."cout << "reading through getline: " << testing << endl;// "And awesome" will still be in buffergetline(cin, testing, '.'); // Input a line "String is great. And awesome."cout << "reading through getline: " << testing << endl;return 0;}
Enter the input below
Instruction: Add the following strings inside the input textbox above:
String is great.String is great. And awesome.
Complete implementation
Here’s the complete code of the above-mentioned examples:
#include<iostream>#include<string>using namespace std;int main(){string a = {'C','+','+'};string b = "C++";cout << "Before Updating..."<<endl;cout << "a: "<<a<<endl; // We will discuss below how this print is working.cout << "b: "<<b<<endl;a[0] = 'A'; // a string variable allows array like accessb[0] = 'B';cout << "\nAfter Updating..."<<endl;cout << "a: "<<a<<endl; // it displays exactly like any other variablecout << "b: "<<b<<endl; //a = b;// string even allows assignment statement (which was not allowed in CString)cout << "\nAfter a = b..."<<endl;cout << "a: "<<a<<endl; // it displays exactly like any other variablecout << "b: "<<b<<endl; //// string can easily be traversed throughcout << "Taking input from console"<<endl;cout << "\nstring traversal..."<<endl;for(int i=0; i<a.size(); i++){cout << a[i] ;if(i!=a.size()-1)cout<<" . ";}cout << endl;// strings can be re-assigned without loop of copyingstring c = "Destroy";string d = "Earth";cout << "\nInitial Msg"<<endl;cout << "c = "<<c<<endl;cout << "d = "<<d<<endl;cout << "\nAfter re-assignment"<<endl;c = "Save";cout << "c = "<<c<<endl;cout << "d = "<<d<<endl;// string concatenation is just like addition of variablescout << "\nConcatenation"<<endl;string sp = " ";string msg = c+sp+d;cout << msg<<endl;// Comparison is just like comparing two numberscout << "\nComparing the strings...!"<<endl;b = "abc";c = "def";cout << "\n1. \n";cout <<b<<"<"<<c<<": "<<(b<c)<<endl;cout <<b<<">"<<c<<": "<<(b>c)<<endl;cout <<b<<"=="<<c<<": "<<(b==c)<<endl;b = "def";c = "abc";cout << "\n2. \n";cout <<b<<"<"<<c<<": "<<(b<c)<<endl;cout <<b<<">"<<c<<": "<<(b>c)<<endl;cout <<b<<"=="<<c<<": "<<(b==c)<<endl;b = "abc";c = "abc";cout << "\n3. \n";cout <<b<<"<"<<c<<": "<<(b<c)<<endl;cout <<b<<">"<<c<<": "<<(b>c)<<endl;cout <<b<<"=="<<c<<": "<<(b==c)<<endl;b = "abc";c = "ABC";// the comparison works based on ASCII, and remember int('A')=65 and int('a')=97cout << "\n4. \n";cout <<b<<"<"<<c<<": "<<(b<c)<<endl;cout <<b<<">"<<c<<": "<<(b>c)<<endl;cout <<b<<"=="<<c<<": "<<(b==c)<<endl;// Taking Input from the console or filecin>>c; // it will ignore all the spaces in the beginning// and then this keep reading characters one by one and// and concatenate it inside c until it finds a delimiter character// i.e. {' ', '\t', '\n', '\r'}cin>>d;cout << "c: "<< c << endl<<" d: "<< d << endl;string testing;getline(cin, testing);cout << "reading through getline: " << testing << endl;return 0;}
Enter the input below