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:
#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; }
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 :RamTo download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions