std::string class in C++


The string is also defined in the std namespace. To use strings in this way, we need to include the header since it is declared in the header. We include it by writing. They are declare variables of type std : : string as follows.

The program demonstrates various operations that can be performed on strings in C++. Here is a brief explanation of each section of the program:

  • Creating and Printing a String The program starts by creating a string a with the value "Welcome To Tutor Joes" and then printing it to the console using the cout statement.
  • String Concatenation Two strings firstName and lastName are defined and concatenated using the + operator to create a full name. The append function is then used to achieve the same result and the output is printed to the console.
  • String Access A string name is defined and printed to the console. The first character of the string is accessed using the subscript operator [0] and printed to the console. The value of the first character is then changed to 'R' and the updated string is printed to the console.
  • Input Functions Two different ways of taking input for a string are demonstrated. First, the cin statement is used to read a string from the console. Then, the getline function is used to read a string with spaces from the console.
  • Capacity Functions A string str is defined and its size, length, and maximum size are printed to the console using the size(),length(), and max_size() functions respectively.
  • Iterator Functions Two for-loops are demonstrated to iterate through a string str. The first loop uses a regular iterator and the second loop uses a reverse_iterator to print the string in reverse order.
  • Manipulating Functions Two strings x and y are defined and swapped using the swap function. The values of x and y are printed to the console before and after the swap operation.

Source Code

#include<iostream>
using namespace std;
/*
C++ String
    Input Functions
    Capacity Functions
    Iterator Functions
    Manipulating  Functions
*/
int main()
{
 
    //string a="Welcome To Tutor Joes";
    string a("Welcome To Tutor Joes");
    cout<<a<<endl;
 
 
    //String Concatenation
    string firstName="Tutor";
    string lastName="Joes";
    cout<<firstName+" "+lastName<<endl;
    string fullName=firstName.append(lastName);
    cout<<fullName<<endl;
 
    //String Access
    string name="Sam Kumar";
    cout<<name<<endl;
    cout<<name[0]<<endl;
    name[0]='R';
    cout<<name<<endl;
	 //Input Functions
    //-------------------------------------------------
	string str;
    cout<<"Enter The String : ";
    cin>>str;
    cout<<"String  : "<<str<<endl;
    fflush(stdin);
    cout<<"Enter The String : "<<endl;
    getline(cin,str);
    cout<<"String  : "<<str;
	string str;
    cout<<"Enter The String : ";
    cin>>str;
    str.push_back('s');
    cout<<str<<endl;
    str.pop_back();
    cout<<str<<endl;
 
	//Capacity Functions
    //-------------------------------------------------
    string str("Tutor Joes");
    cout<<str<<endl;
    cout<<"Size     : "<<str.size()<<endl;
    cout<<"Length   : "<<str.length()<<endl;
    cout<<"Max Size : "<<str.max_size()<<endl;
 
	//Iterator Functions
    //-------------------------------------------------
    string str = "Tutor Joes";
    string::iterator it;
    for(it=str.begin();it!=str.end();it++)
        cout<<*it<<endl;
    cout<<"------------------------"<<endl;
    string::reverse_iterator it2;
    for(it2=str.rbegin();it2!=str.rend();it2++)
        cout<<*it2<<endl;
//Manipulating Functions
    //-------------------------------------------------
    string x="Ram";
    string y="Sam";
    cout<<"Before X :"<<x<<endl;
    cout<<"Before Y :"<<y<<endl;
    x.swap(y);
    cout<<"After X :"<<x<<endl;
    cout<<"After Y :"<<y<<endl;
 
    return 0;
}
 

Output

Welcome To Tutor Joes

//String Concatenation
Tutor Joes
TutorJoes

//String Access
Sam Kumar
S
Ram Kumar

//Input Functions
Enter The String : Tutor
String  : Tutor
Enter The String :
Joes
String  : Joes
Enter The String : Computer
Computers
Computer

//Capacity Functions
Tutor Joes
Size     : 10
Length   : 10
Max Size : 9223372036854775807

//Iterator Functions
T
u
t
o
r

J
o
e
s
------------------------
s
e
o
J

r
o
t
u
T

//Manipulating Functions
Before X :Ram
Before Y :Sam
After X :Sam
After Y :Ram
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples