Professional Documents
Culture Documents
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char *line = "short line for testing";
// with no arguments
string s1;
s1 = "Anatoliy";
cout << "s1 is: " << s1 << endl;
// copy constructor
string s2 (s1);
cout << "s2 is: " << s2 << endl;
// one argumen
string s3 (line);
cout << "s3 is: " << s3 << endl;
// 1 - C++ string
// 2 - start position
// 3 - number of characters
string s5 (s3,6,4); // copy word 'line' from s3
cout << "s5 is: " << s5 << endl;
// 1 - number characters
// 2 - character itself
string s6 (15,'*');
cout << "s6 is: " << s6 << endl;
// 1 - start iterator
// 2 - end iterator
string s7 (s3.begin(),s3.end()-5);
cout << "s7 is: " << s7 << endl;
return 0;
}
OUTPUT:
// s1 is: Anatoliy
// s2 is: Anatoliy
// s3 is: short line for testing
// s4 is: short line
// s5 is: line
// s6 is: ***************
// s7 is: short line for te
// s8 is: Anatoliy
getline 1.
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int main ()
{
string str;
cout << "Enter string (EOL = $) : ";
getline (cin, str, '$');
cout << "Str is : " << str << endl;
ifstream In("data.dat");
vector v;
cout << endl << "Read data from file" << endl;
while ( ! In.eof() )
{
getline (In, str);
v.push_back(str);
}
copy (v.begin(),v.end(),
ostream_iterator(cout,"\n"));
cout << endl;
return 0;
}
OUTPUT:
// Enter string (EOL = $) : Str is : first line
// second line$
//
// Read data from file
// file: "data.dat"
// second line
// last line
<< >> operators 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str;
return 0;
}
OUTPUT:
// Enter string for testing : first
// String is : first
// Enter string for testing (d to quit) : second
// String is : second
// Enter string for testing (d to quit) : third
// String is : third
// Enter string for testing (d to quit) :
+ += = operators 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "Hello";
cout << "str is : " << str << endl;
str += ",";
str += ' ';
cout << "str is : " << str << endl;
string s;
s = str + "World";
cout << "s is : " << s << endl;
char ch = '!';
s += ch;
cout << "s is : " << s << endl;
return 0;
}
OUTPUT:
// str is : Hello
// str is : Hello,
// s is : Hello, World
// s is : Hello, World!
append 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "Nobody is perfect";
string s = ""; // empty string
char *ch = "abcdef";
s.append(inpIt1,inpIt2);
cout << "s is : " << s << endl;
// appends three !
s.append(3,'!');
cout << "s is : " << s << endl;
return 0;
}
OUTPUT:
// s is : Nobody
// s is : Nobody is perfect
// s is : Nobody is perfect!!!
// s is : Nobody is perfect!!!abc
// s is : Nobody is perfect!!!abcabc
assign 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "Nobody is perfect";
string s = "";
char *ch = "Robert Frost";
at 1.
// returns s[pos]
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string s = "Nobody is perfect";
// Returns s[pos]
for ( int pos = 0; pos < s.length(); ++pos )
cout << s.at(pos) << " ";
cout << endl;
return 0;
}
OUTPUT:
// N o b o d y i s p e r f e c t
begin 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "C++ is best computer language";
string::iterator It = str.begin();
while ( It != str.end() )
{
if ( *It == ' ' )
*It = '\n';
cout << *It++;
}
cout << endl;
return 0;
}
OUTPUT:
// C++
// is
// best
// computer
// language
c_str 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "Anatoliy";
char *ary = new char[str.length()+1];
return 0;
}
OUTPUT:
// Anatoliy
capacity 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "C++ is best computer language";
string::size_type cap;
cap = str.capacity();
cout << "Capacity of str is: " << cap << endl;
cout << "Size of str is : " << str.size()
<< endl;
cout << "Length of str is : " << str.length()
<< endl;
cout << "Capacity of str is: " << cap << endl;
cout << "Size of str is : " << str.size()
<< endl;
cout << "Length of str is : " << str.length()
<< endl;
return 0;
}
OUTPUT:
// Capacity of str is: 32
// Size of str is : 29
// Length of str is : 29
// Resize the str for 50 character
// Capacity of str is: 64
// Size of str is : 50
// Length of str is : 50
compare 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str1 = "string";
string str2 = "String";
string str3 = "second string";
char ch[] = "first string";
return 0;
}
OUTPUT: GNU compiler
// string str1 is : string
// string str2 is : String
// char ary ch is : first string
// string str3 is : second string
//
// 1.
// String str1 is not equal to string str2
// 2.
// String str1 is equal to array of char "string"
// 3.
// Part of string str3 is equal to str1
// 4.
// Part of string str3 is equal to C string "string"
// 5.
// 6.
// 7.
// 8.
copy 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "First Name: Robert";
char fname[255];
cout << "str is: " << str << endl;
int n = str.find(':');
return 0;
}
OUTPUT:
// str is: First Name: Robert
// fname is: First Name:
empty 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "*******";
while ( ! str.empty() )
{
cout << str << endl;
str.erase(str.end()-1);
}
cout << endl;
return 0;
}
OUTPUT:
// *******
// ******
// *****
// ****
// ***
// **
// *
end 1.
// returns an iterator porsitioned immediately
// after the last character in string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string s;
string str = "*************************";
size_t pos = str.length();
while ( pos )
{
s.assign ( str.begin(),str.end() - pos+1);
cout << s << endl;
pos -= 5;
}
return 0;
}
OUTPUT:
// *
// ******
// ***********
// ****************
// *********************
erase 1.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main ()
{
string str, s;
return 0;
}
OUTPUT:
// str is: abcdefghijklmnopqrstuvwxyz
// s is: abcdefghijklmnopqrstuvwxyz
// Erased range fron str : nopqrstuvwxyz
// Erased range from s : abcdefghijklm
//
// Erase one, second character from s
// s is: acdefghijklm
// s is: fghijklm
find 1.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main ()
{
string str("C++ is best language");
int pos1, pos2; // size_t or size_type
// work not correct
// search for first string "best" inside of str
// default position is 0
pos1 = str.find ("best");
cout << "Word best is found on position " << pos1+1
<< endl;
find_first_not_of 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str("C++ is best language");
cout << "str is: " << str << endl;
int n = str.find_first_not_of("aeiouAEIOU");
return 0;
}
OUTPUT:
// str is: C++ is best language
// First consonant found at 1 position
find_first_not_of 2.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str("C++ is best language");
cout << "str is: " << str << endl;
return 0;
}
OUTPUT:
// str is: C++ is best language
// first not of space character found at position 8
find_first_not_of 3.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str("C++ is best language");
string s = "C++";
return 0;
}
OUTPUT:
// str is: C++ is best language
// first not 'C' is found at position 2
// first not of C++ is found at position 4
find_first_of 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str("C++ is best language");
string s = "be";
return 0;
}
OUTPUT:
// str is: C++ is best language
// first 'be' found at position 8
// first character 'l' found at position 13
// charary[] = " bea"
// first character from charary found at position 4
// Note: position 4 is space
// first character from charary found at position 4
find_last_not_of 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str("C++ is best language");
string s = "langue";
int pos = str.length()-1;
cout << "str is: " << str << endl;
return 0;
}
OUTPUT:
// str is: C++ is best language
// last_not_of 'langue' found at position 12
// last_not_of 'e' found at position 19
// last_not_of 'be' found at position 19
find_last_of 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str("C++ is best language");
string s = "g";
cout << "str is: " << str << endl;
cout << "s is: " << s << endl;
int n = str.find_last_of(s);
cout << "last_of '" << s << "' faund"
<< " at position " << n+1 << endl;
n = str.find_last_of(' ');
cout << "last_of ' ' faund"
<< " at position " << n+1 << endl;
n = str.find_last_of(" la");
cout << "last_of \" la\" faund"
<< " at position " << n+1 << endl;
return 0;
}
OUTPUT:
// str is: C++ is best language
// s is: g
// last_of 'g' faund at position 19
// last_of ' ' faund at position 12
// last_of " la" faund at position 18
insert 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "C++ language";
string s = "is best";
char ch[] = "C++ language";
n = s.find('l');
s.insert(s.begin()+n,' ');
cout << "s is: " << s << endl;
length 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "C++ is best computer language";
cout << "str is: " << str << endl;
return 0;
}
OUTPUT:
// str is: C++ is best computer language
// Length of str is : 29
max_size 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "C++ is best computer language";
cout << "str is: " << str << endl;
return 0;
}
OUTPUT:
// str is: C++ is best computer language
// max_size of str is: 4294967294
rbegin 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "C++ is best computer language";
cout << "str is: " << str << endl;
return 0;
}
OUTPUT:
// str is: C++ is best computer language
// egaugnal retupmoc tseb si ++C
replace 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "STL is created from Dennis Ritchie";
string s1 = "was";
string s2 = "developed";
string s3 = "Stepanov alexander";
cout << "str is: " << str << endl;
return 0;
}
OUTPUT:
// str is: STL is created from Dennis Ritchie
// replace 'is' for 'was'
// str is: STL was created from Dennis Ritchie
// replace 'created' for 'developed'
// str is: STL was developed from Dennis Ritchie
// replace 'Dennis' for 'alexander'
// str is: STL was developed from alexander Ritchie
// replace 'from' for 'by'
// str is: STL was developed by alexander Ritchie
// replace 'a' for 'A' (alexander)
// str is: STL was developed by Alexander Ritchie
// replace 'Ritchie' for 'Stepanov'
// str is: STL was developed by Alexander Stepanov
reverse 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "Anatoliy Urbanskiy";
cout << str.reverse() << endl;
return 0;
}
OUTPUT:
resize 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "Alexander Stepanov";
cout << "str is: " << str << endl;
cout << "size of str is: "
<< str.size() << endl;
str.resize(11);
return 0;
}
OUTPUT:
// str is: Alexander Stepanov
// size of str is: 18
// after str.resize(11)
// str is: Alexander S
// size of str is: 11
// after str.resize(9,'.')
// str is: Alexander S.........
// size of str is: 20
rfind 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "We go step by step to the target";
string s1 = "step";
cout << "str is: " << str << endl;
cout << "s1 is: " << s1 << endl;
return 0;
}
OUTPUT:
// str is: We go step by step to the target
// s1 is: step
// int n1 = str.find(s1)
// n1 = 7
// int n2 = str.rfind(s1)
// n2 = 15
// n3 = str.rfind(s1,n2-1)
// n3 = 7
// n1 = str.rfind('t')
// n1 = 32
// n2 = str.rfind('t',n1-1)
// n2 = 27
// char ch[] = "step"
// n1 = str.rfind(ch)
// n1 = 15
// n2 = str.rfind("stabc",10,2)
// n2 = 7
size 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "We go step by step to the target";
string::size_type size = str.size();
return 0;
}
OUTPUT:
// str is: We go step by step to the target
// size of str = 32
substr 1.
// str.subsr(pos,n);
// returns a copy of the substring consisting
// of n characters from str, beginning at position pos
// (default value 0); if n is too large or is omitted,
// characters are copied only until the end of s is
// reached
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "We go step by step to the target";
cout << "str is: " << str << endl;
int n = str.find("step");
string s = str.substr(n);
cout << "s is: " << s << endl;
s = str.substr(n,12);
cout << "s is: " << s << endl;
return 0;
}
OUTPUT:
// str is: We go step by step to the target
// s is: step by step to the target
// s is: step by step
swap 1.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str1 = "Robert";
string str2 = "Forest";
cout << "str1 is: " << str1 << endl;
cout << "str2 is: " << str2 << endl;
cout << endl;
str1.swap(str2);
cout << "str1 is: " << str1 << endl;
cout << "str2 is: " << str2 << endl;
return 0;
}
OUTPUT:
// str1 is: Robert
// str2 is: Forest
//
// str1.swap(str2)
//
// str1 is: Forest
// str2 is: Robert