Professional Documents
Culture Documents
String
String
String
1
Manipulating String
2
Creating string objects
#include<string>
….. s3+="Bye"; Output:
int main() cout<<s3<<endl;
{ cout<<s4<<endl; ABC
string s1; string s7(s4); ABCABC
string s2("ABC"); s2=s4+"Bad";
string s3=s2; cout<<s2<<endl;
ABCBye
cout<<s3<<endl; string s8; Hello
string s4="Hello";
string s5,s6;
s8=“Good morning”; HelloBad
// reading a word s2.assign(s8,1,6); ood mo
cin>>s5;
// reading a line
cout<<s2<<endl; od m
getline(cin,s6);
s2+=s3; s2.assign(s8,2,5);
cout<<s2<<endl; cout<<s2<<endl;
return 0;
}
3
Manipulating string objects
#include<cstring> Output:
int main() Good Hello morning
{ Good Happy morning
string s1="Hello "; Good morning
string s2=“Good morning";
string s3="Bad";
s2.insert(5,s1);
cout<<s2<<endl;
s2.replace(5,5,"happy ");
cout<<s2<<endl;
s2.erase(5,6);
cout<<s2<<endl;
return 0;
}
4
Relational operator
#include<string> Output:
…. s1 bigger
int main()
{
string s1(“hello");
string s2("good morning");
if(s1>s2)
cout<<“s1 bigger\n";
else
cout<<"s2 bigger\n";
return 0;
5
}
String compare
Output:
#include<iostream> -1
#include<string> -1
using namespace std; 0
int main()
{
string s1("hello");
string s2("good morning");
int x=s1.compare(s2);
cout<<x<<endl;
cout<<s2.compare(s1)<<endl;
string s3 = "hello";
cout<<s1.compare(s3)<<endl;
return 0;
6
}
String erase
Output:
#include<iostream> Hello w
#include<string>
using namespace std;
int main()
{
string s1("Hello world");
//erase all chars from index 7
s1.erase(7);
cout<<s1;
return 0;
}
7
String swap
8
From “null-terminated string” to
“string object”
int main() Output:
{ Hello
char *st="Hello";
string s1(st);
cout<<s1<<endl;
return 0;
}
9
From “string object” to
“null-terminated string”
int main() Output:
{ Hello
string s1="Hello";
const char *st=s1.c_str();
cout<<st<<endl;
return 0;
}
10
String characteristics
int main() Output:
{ 13
string str("one two three"); 13
cout<<str.size()<<endl; 13
cout<<str.length()<<endl; 4294967293
cout<<str.capacity()<<endl; false
cout<<str.max_size()<<endl;
cout<<boolalpha<<str.empty()<<endl;
return 0;
}
11
String characteristics: resize()
int main() Output:
{ 20
string str("one two three"); 20
str.resize(20); 26
cout<<str.size()<<endl; 4294967293
cout<<str.length()<<endl; false
cout<<str.capacity()<<endl;
cout<<str.max_size()<<endl;
cout<<boolalpha<<str.empty()<<endl;
return 0;
}
12
String characteristics : reserve()
int main() Output:
{ 13
string str("one two three"); 13
str.reserve(20); 26
cout<<str.size()<<endl; 4294967293
cout<<str.length()<<endl; false
cout<<str.capacity()<<endl;
cout<<str.max_size()<<endl;
cout<<boolalpha<<str.empty()<<endl;
return 0;
}
13
String & exception
#include<exception> Output:
#include<string> Hello world
…..
int main() Hello world
{ invalid string position
string s1="Hello world";
int i;
for(i=0;i<=s1.size();i++)
cout<<s1[i];
cout<<endl;
try{
for(i=0;i<=s1.size();i++)
cout<<s1.at(i);
cout<<endl;
}
catch(out_of_range e)
{
cout<<endl;
cout<<e.what()<<endl;
}
return 0;
}
14
Accessing characters of a string
int main() Output:
{
string str("one two three");
4
string::size_type r; 0
string::size_type n; 2
n=str.find("two");
cout<<n<<endl;
two th
cout<<str.find_first_of("teo")<<endl;
cout<<str.find_first_not_of("no")<<endl;
cout<<str.substr(4,6)<<endl;
return 0;
}
15
Replacing Substring
int main()
{
string s1="Think big Act big Get big";
int position = s1.find("big");
while(position != string::npos)
{
s1.replace(position,3,"small");
position = s1.find("big",position+1);
}
cout<<s1<<endl;
Output:
return 0;
} Think small Act small Get small
Note: Member function find returns string::npos when the search character or
substring is not found 16
Exercise
1. Implement a function with the following specification:
string censor(const string& s, string objectionable[],
int size)
where s is the string to be censored, objectionable is an array of
strings that need to be censored, and size is the size of the array
objectionable. The function returns a string with all the words in the string
s which are contained in objectionable replaced with asterisks, “*”.
e.g. The string may be “This is the string to be censored”. If the word “the” is in
the objectionable array, then the string “This is *** string to be censored.”
is returned.
17