ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

C++ Chapter 10: C++ all about Strings

prodevelopertutorial January 27, 2020

In this chapter we shall learn about below topics:

  1. Introduction
  2. String object in C++
  3. String relational operators
  4. String modification functions
  5. String attribute functions
  6. Accessing string elements
  7. 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.

 

  1. insert() function is used to insert a string at a specified position.

Syntax:

string_1.insert(<position>, <string_to_insert>);

 

  1. erase() function is used to erase the specified characters.

Syntax:

string_1.insert(<position_start>, <position_end>);

 

  1. replace() function is used to replace the specified characters.

Syntax:

string_1.insert(<position_start>, <position_end>, <string_2>);

 

  1. 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.

  1. 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()

 

  1. 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()

 

  1. capacity() function is used to get the capacity of string object.

Syntax:

string_1.capacity()

 

  1. 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++

 

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT