ALL ABOUT STRING CLASS IN ONE PLACE

String class
Example 1:Initializing string
#include<iostream>
#include<string>
using namespace std;

int main(){
       string s;
       if(s.empty()){
       cout<<"string is empty "<<endl;
       }

string s1(6,'B');//add 6 B's in string s1
string s2("abu bakar");//store abu bakar in string s2
string s3(s2,1,8);//stores2 from 1st character till 8 characters in s3
string s4("you are welcome ",7);//store string till 7th character in s4
string s5;//alternative way of string assignment
s5=" just initializing ";
cout<<endl<<"now we start printing each string "<<endl;
cout<<" s1 = "<<s1<<endl;
cout<<" s2 = "<<s2<<endl;
cout<<" s3 = "<<s3<<endl;
cout<<" s4 = "<<s4<<endl;
cout<<" s5 = "<<s5<<endl;

}






Example 2: comparison of strings


#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1,s2,s3,s4,s5,s6,s7;
s1="Pakistan";
s2="India";
s3="Australia";
s4="Canada";
s5="Pakistan";
s6="pakistan";
s7="lovePakistan";
cout<<"s1 = "<<s1<<"\ns2 = "<<s2<<endl<<"s3 "<<s3<<endl
       <<"s4 = "<<s4<<endl<<"s5 = "<<s5<<endl<<"s6 = "<<s6<<endl<<"s7 = "<<s7<<endl;
cout<<"s1.compare(s2) = "<<s1.compare(s2)<<endl;
cout<<"s1.compare(s3) = "<<s1.compare(s3)<<endl;
cout<<"s1.compare(s4) = "<<s1.compare(s4)<<endl;
cout<<"s1.compare(s5) = "<<s1.compare(s5)<<endl;//compare matched
cout<<" s1.compare(s6) = "<<s1.compare(s6)<<endl;


}





Example 3: assignment in strings and using size() and capacity() functions
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1,s2,s3,s4;
s2="i love programming ";
s3="C++ is the best programming language ";
s4="work hard so that you may succeed ";

cout<<" s1.size() = "<<s1.size()<<endl;
cout<<" s1.capacity() "<<s1.capacity()<<endl;

cout<<"Before making changes "<<endl<<" s1 = "<<s1<<endl<<" s2 = "<<s2
       <<endl<<" s3 = "<<s3<<endl;

s1.assign(s3);
s2.assign(s3,7,18);

cout<<"after making changes "<<endl;
cout<<endl<<" s1 = "<<s1<<endl<<" s2 = "<<s2
       <<endl<<" s3 = "<<s3<<endl;

cout<<"character at position 4 in s4 = "<<s4.at(3)<<endl;
cout<<"character at position 4 in s4 = "<<s4[3]<<endl;
}










Example 4:substring of a string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1,s3,s4;

s1="programming is easy ";
cout<<" s1 = "<<s1<<endl;
cout<<" s1.substr(2,8) = "<< s1.substr(2,8)<<endl<<endl;//1 way of getting substring

string s2(s1,2,8);//2nd way of getting substring
cout<<"after doing : string s2(s1,2,8) "<<endl;
cout<<"s2 = " <<s2<<endl<<endl;


s3=s1.substr(2,8);//assigning s3 substring of s1
cout<<"after doing : s3=s1.substr(2,8) "<<endl;
cout<<"s3 = " <<s3<<endl<<endl<<endl;

s4="programming is difficult ";
cout<<"Before erasing s4 = "<<s4 <<endl;
cout<<"after erasing s4 = "<<s4.erase()<<endl;
if(s4.empty()){
cout<<"s4 is empty "<<endl;
}
}







Example 5:concatenation of string , at() function,modified comparision()
#include<iostream>
#include<string>
using namespace std;
int main(){
string s1,s2,s3,s4,s5;
s1="cat";
s2=s1+"comb";
s3=" you are a lucky person ";
s4=" what do you do ";
cout<<" s1 = "<<s1<<endl<<" s2 = "<<s2<<endl<<" s3 = "<<s3<<endl<<" s4 = "<<s4<<endl<<endl;

s3.append(s4,4,7);
s2.swap(s1);
s5=s4.substr(2,8);
cout<<"after making following changes "<<endl;
cout<<"s3.append(s4,4,7)"<<endl<<"s2.swap(s1)"<<endl<<"s5=s4.substr(2,8)"<<endl<<endl;
cout<<" s1 = "<<s1<<endl<<" s2 = "<<s2<<endl<<" s3 = "<<s3<<endl<<" s4 = "<<s4<<endl<<" s5 = "<<s5<<endl;


string s6="i love pakistan ";
string s7="pakistan";
if((s7.compare(0,8,s6,6,8)==0));{
cout<<endl<<"comparision matched "<<endl<<endl;
}

cout<<"demonstrating at() function"<<endl<<endl;
for(int x=0;x<=s7.size()-1;x++){
cout<<x<<" = "<<s7.at(x)<<endl;
}


}




Example 6: string erasing and finding functions

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int main(){
string s1("apple is green,mango is yellow,orange is orange,banana is yellow");
cout<<" in string = "<<s1<<" 'is' is found at "<<s1.find("is")<<endl<<endl;
cout<<" in string = "<<s1<<" 'is' is found at from last  "<<s1.rfind("is")<<endl<<endl;
cout<<"string1.find_first_of(',') is found at location = "<<s1.find_first_of(",")<<endl;
cout<<"string1.find_last_of(',')= "<< s1.find_last_of(',')<<endl<<endl;
cout<<"string1.find_first_not_of('aiple') = "  <<s1.find_first_not_of("aiple")<<endl<<endl;
cout<<endl<<endl<<"before replacement string s1 = "<<s1<<endl<<endl;
int position=s1.find(",");
while(position!=string::npos){
s1.replace(position,1,";;");

position=s1.find(",",position+1);
}
cout<<endl<<endl<<"after replacement string s1 = "<<s1<<endl<<endl;

}



Previous
Next Post »

Comments:

Disqus Shortname