In this chapter we shall learn about below topics:
- Introduction
- String object in C++
- String relational operators
- String modification functions
- String attribute functions
- Accessing string elements
- Comparing string functions
1. Introduction
String is a sequence of characters. Every string should be terminated by NULL character ‘\0’. In C style, an extra space need to be allocated to hold a NULL value.
C style string:
char website_name [21] = “prodevelopertutorial”;
C++ provides “string” object that makes string manipulation easy.
2. String object in C++
“string” is the keyword used to declare a string variable.
Below are various examples on how to use strings:
string text; //Creates an empty string string language("c++"); //assigns "c++" to the variable language string_1 = string_2 //assign value of string_2 to string_1 string_1 = "c++" + "tutorils" // string concatenation
Example on string usage in C++
/* * File : strings_example.cpp * Author : ajay.thousand@gmail.com * Copyright: @ prodevelopertutorial.com */ #include<iostream> #include<string> using namespace std; int main() { string string_1 = "c++"; string string_2(" prodevelopertutorial"); string string_3; cout<<"The string_1 is "<<string_1<<endl; cout<<"The string_2 is "<<string_2<<endl; //string concatenation string_1 += string_2; cout<<"The string_1 after concatenation is \""<<string_1<<"\""<<endl; //string assignment string_3 = string_2; cout<<"The string_3 after assignment is "<<string_3<<endl; return 0; }
Output:
The string_1 is c++ The string_2 is prodevelopertutorial The string_1 after concatenation is "c++ prodevelopertutorial" The string_3 after assignment is prodevelopertutorial
3. String relational operators
We can use relational operators (shown below) to compare 2 string are same or not.
== operator checks if both strings are same
> checks if string_1 is greater than string_2
Example of relational operators:
/* * File : strings_relational_operator_example.cpp * Author : ajay.thousand@gmail.com * Copyright: @ prodevelopertutorial.com */ #include<iostream> #include<string> using namespace std; int main() { string string_1 = "c++"; string string_2("c++"); string string_3 = "C++"; cout<<"The string_1 is = "<<string_1<< " and string_2 is = "<<string_2<<" and string_1 == string_2 value is = "<< (string_1 == string_2) <<endl; cout<<"The string_1 is = "<<string_1<< " and string_3 is = "<<string_3<<" and string_1 == string_3 value is = "<< (string_1 == string_3) <<endl; cout<<"The string_1 is = "<<string_1<< " and string_2 is = "<<string_2<<" and string_1 > string_2 value is = "<< (string_1 > string_2) <<endl; cout<<"The string_1 is = "<<string_1<< " and string_3 is = "<<string_3<<" and string_1 > string_3 value is = "<< (string_1 > string_3) <<endl; return 0; }
Output:
The string_1 is = c++ and string_2 is = c++ and string_1 == string_2 value is = 1 The string_1 is = c++ and string_3 is = C++ and string_1 == string_3 value is = 0 The string_1 is = c++ and string_2 is = c++ and string_1 > string_2 value is = 0 The string_1 is = c++ and string_3 is = C++ and string_1 > string_3 value is = 1
4. String modification functions
Below are the functions used to modify strings.
- insert() function is used to insert a string at a specified position.
Syntax:
string_1.insert(<position>, <string_to_insert>);
- erase() function is used to erase the specified characters.
Syntax:
string_1.insert(<position_start>, <position_end>);
- replace() function is used to replace the specified characters.
Syntax:
string_1.insert(<position_start>, <position_end>, <string_2>);
- append() function is used to append another string at the end of first string.
Syntax:
string_1.append(string_2);
Example for string Modification function:
/* * File : strings_modification_example.cpp * Author : ajay.thousand@gmail.com * Copyright: @ prodevelopertutorial.com */ #include<iostream> #include<string> using namespace std; int main() { string string_1 = "c++ "; string string_2 = "Tutorial"; string string_3 = "prodevelopertutorial.com"; string string_4 = "Tutorial on prodevelopertutorial"; string string_5 = "c++ "; cout<<"The string_1 is = "<<string_1<< " and string_2 is = "<<string_2<<" and string_1.insert(4, string_2) value is = "<<(string_1.insert(4, string_2)) <<endl; cout<<"The string_3 is = "<<string_3<< " and string_3.erase(20, 24) value is = "<<(string_3.erase(20, 24)) <<endl; cout<<"The string_3 is = "<<string_3<< " and string_3.replace(3, 9, \"DEVELOPER\") value is = "<<(string_3.replace(3, 9, "DEVELOPER")) <<endl; cout<<"The string_5 is = "<<string_5<< " and string_4 is = "<<string_4<<" and string_5.append(string_4) value is = "<<(string_5.append(string_4)) <<endl; return 0; }
Output:
The string_1 is = c++ and string_2 is = Tutorial and string_1.insert(4, string_2) value is = c++ Tutorial The string_3 is = prodevelopertutorial.com and string_3.erase(20, 24) value is = prodevelopertutorial The string_3 is = prodevelopertutorial and string_3.replace(3, 9, "DEVELOPER") value is = proDEVELOPERtutorial The string_5 is = c++ and string_4 is = Tutorial on prodevelopertutorial and string_5.append(string_4) value is = c++ Tutorial on prodevelopertutorial
5. String attribute functions
Below are the functions used to get string attributes.
- size() function is used to get the size of string object. It gives the number of bytes occupied by that string object.
Syntax:
string_1.size()
- length() function is used to get the length of string object. It gives the number of characters present in that string object.
Syntax:
string_1.length()
- capacity() function is used to get the capacity of string object.
Syntax:
string_1.capacity()
- max_size() function is used to get the maximum size of string object.
Syntax:
string_1.max_size()
Example for string attribute functions:
/*
* File : strings_attributes_example.cpp
* Author : ajay.thousand@gmail.com
* Copyright: @ prodevelopertutorial.com
*/
#include<iostream>
#include<string>
using namespace std;
int main()
{
string string_1;
cout<<"The value of string_1 is = "<<string_1<<" and size of string_1 is = "<<string_1.size()<<endl;
string_1 = "C++";
cout<<"The value of string_1 is = "<<string_1<<" and size of string_1 is = "<<string_1.size()<<endl<<endl;
string string_2;
cout<<"The value of string_2 is = "<<string_2<<" and length of string_2 is = "<<string_2.length()<<endl;
string_2 = "C++";
cout<<"The value of string_2 is = "<<string_2<<" and length of string_2 is = "<<string_2.length()<<endl<<endl;
string string_3;
cout<<"The value of string_3 is = "<<string_3<<" and capacity of string_3 is = "<<string_3.capacity()<<endl;
string_3 = "C++";
cout<<"The value of string_3 is = "<<string_3<<" and capacity of string_3 is = "<<string_3.capacity()<<endl<<endl;
string string_4;
cout<<"The value of string_4 is = "<<string_4<<" and max_size of string_4 is = "<<string_4.max_size()<<endl;
return 0;
}
Output:
The value of string_1 is = and size of string_1 is = 0 The value of string_1 is = C++ and size of string_1 is = 3 The value of string_2 is = and length of string_2 is = 0 The value of string_2 is = C++ and length of string_2 is = 3 The value of string_3 is = and capacity of string_3 is = 22 The value of string_3 is = C++ and capacity of string_3 is = 22 The value of string_4 is = and max_size of string_4 is = 18446744073709551599
6. Accessing string element
1. at(): function is used to get the character at that index.
Example:
string_1 = “c++” string_1.at(0) will return “c”
2. substr() function is used to get the substring from “start_index” to “end_index”.
Example:
string_1. substr (2, 4)
3. find() function is used to check if a “sub string” is present in the “string” or not.
Example:
string_1. find (“hi”)
4. find_first_of() : it is used to find the first occurrence of the given character.
Example:
string_1. find_first_of (“/”)
5. find_last_of() : it is used to find the last occurrence of the given character.
Example:
string_1. find_last_of (“/”)
Example of string accessing:
/* * File : accessing_strings_example.cpp * Author : ajay.thousand@gmail.com * Copyright: @ prodevelopertutorial.com */ #include<iostream> #include<string> using namespace std; int main() { string string_1 = "prodevelopertutorial"; cout<<"The value of string_1 is = "<<string_1<<" and the character at 2 is = "<<string_1.at(2)<<endl; string string_2 = "c++ tutorial at prodevelopertutorial"; cout<<"The value of string_2 is = "<<string_2<<" and find(\" tutorial\") is = "<<string_1.find("tutorial")<<endl; cout<<"The value of string_1 is = "<<string_1<<" and substr(3, 11) is = "<<string_1.substr(3, 11)<<endl; string string_3 = "a/b/c"; cout<<"The value of string_3 is = "<<string_3<<" and find_first_of(\"/\") is = "<<string_3.find_first_of("/")<<endl; cout<<"The value of string_3 is = "<<string_3<<" and find_last_of(\"/\") is = "<<string_3.find_last_of("/")<<endl; return 0; }
Output:
The value of string_1 is = prodevelopertutorial and the character at 2 is = o The value of string_2 is = c++ tutorial at prodevelopertutorial and find(" tutorial") is = 12 The value of string_1 is = prodevelopertutorial and substr(3, 11) is = developertu The value of string_3 is = a/b/c and find_first_of("/") is = 1 The value of string_3 is = a/b/c and find_last_of("/") is = 3
7. Comparing string functions
1. compare() function is used to compare 2 strings.
2. swap() function is used to swap 2 strings.
Example:
/* * File : compare_and_swap_strings_example.cpp * Author : ajay.thousand@gmail.com * Copyright: @ prodevelopertutorial.com */ #include<iostream> #include<string> using namespace std; int main() { string string_1 = "prodevelopertutorial"; string string_2 = "prodevelopertutorial"; string string_3 = "Prodevelopertutorial"; cout<<"The string_1 is = "<<string_1<< " and string_2 is = "<<string_2<<" and string_1.compare( string_2) value is = "<<(string_1.compare( string_2)) <<endl; cout<<"The string_1 is = "<<string_1<< " and string_3 is = "<<string_3<<" and string_1.compare( string_3) value is = "<<(string_1.compare( string_3)) <<endl; string string_4 = " c++ tutorial"; cout<<"The string_1 is = "<<string_1<< " and string_4 is = "<<string_4<<endl; string_1.swap(string_4); cout<<"After swap"<<endl; cout<<"The string_1 is = "<<string_1<< " and string_4 is = "<<string_4<<endl; return 0; }
Output:
The string_1 is = prodevelopertutorial and string_2 is = prodevelopertutorial and string_1.compare( string_2) value is = 0 The string_1 is = prodevelopertutorial and string_3 is = Prodevelopertutorial and string_1.compare( string_3) value is = 32 The string_1 is = prodevelopertutorial and string_4 is = c++ tutorial After swap The string_1 is = c++