Advertisement
// The insert() function in string class.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str("Hello");
cout<<"The string is : " << str <<"\n";
//Calling insert() function to insert a string into another string.
str.insert(2, "zzz");
cout<<"The modified string : " << str;
}
The string is : Hello,
The modified string : Hezzzllo
// The append() function in string class.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str("Hello,");
cout<<"The string is : " << str <<"\n";
string str2("how are you doing?");
//Calling append() function which appends only part of a string to the end of another string.
str.append(str2, 0, 10);
cout<<"The modified string after calling append() function : " << str;
}
The string is : Hello
The modified string : Hezzzllo
// The erase() function in string class.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str("Welcome");
cout<<"The string is : " << str <<"\n";
//Calling erase() function to erase 2 characters starting from index 3
str.erase(3,2);
cout<<"The modified string after calling erase() function : " << str;
}
The string is : Welcome
The modified string after calling erase() function : Welme
// The replace() function in string class.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str("Ting tong");
cout<<"The string is : " << str <<"\n";
string str2("XYZ");
//Calling replace() function to replace part of a string with another string.
str.replace(0,2,str2);
cout<<"The modified string after calling append() function : " << str;
}
The string is : Ting tong
The modified string after calling replace() function : XYZng tong
Advertisement
// The length() function in string class.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1;
//Calling length() function to check length of an empty string.
cout<< "The length of first string is : " << str1.length() << "\n";
string str2("Welcome");
cout<<"The length of second string is : " << str2.length();
}
The length of first string is : 0
The length of second string is : 7
// The at() function in string class.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1("Welcome");
cout<< "The string value is : " << str1 << "\n";
cout<< "The character at the index 2 in string : " << str1.at(2);
}
The string value is : Welcome
The character at the index 2 in string : l
// The swap() function in string class.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1("Welcome");
cout<< "The string value in the first string : " << str1 << "\n";
string str2("Hello");
cout<< "The string value in the second string : " << str2 << "\n";
//Calling swap() function to swap the values of two strings
str1.swap(str2);
cout<< "The string value in the first string after swap() : " << str1 << "\n";
cout<< "The string value in the second string after swap() : " << str2 << "\n";
return 0;
}
The string value in the first string : Welcome
The string value in the second string : Hello
The string value in the first string after swap() : Hello
The string value in the second string after swap() : Welcome
// The find() function in string class.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1("Welcome");
cout<< "The string is : " << str1 << "\n";
string substr = "come";
cout<< "The substring to find in a string : " <lt; substr;
//Finding the substring "come" in the string
int index = str1.find(str);
cout<< "The index of the found substring in the string is : " << index;
}
The string is : Welcome
The substring to find in a string : come
The index of the found substring in string is : 3
// The substr() function in string class.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1("Welcome");
cout<< "The string is : " << str1 << "\n";
//Finding the substring starting at index 0 and ending at index 2 in the string
string subs= str1.substr(0,2);
cout<< "The found substring in the string is : " << subs;
}
The string is : Welcome
The found substring in the string is : We
// The find_first_of() function in string class.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1("Welcome");
cout<< "The string is : " << str1 << "\n";
//Finding the index of the first occurrence of a character in the string
int index= str1.find_first_of('e');
cout<< "The index of the first occurrence of the character e in string is : " << index;
}
The string is : Welcome
The index of the first occurrence of the character e in string is : 1
// The swap() function in string class.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1("Welcome");
cout<< "The string is : " << str1 << "\n";
//Finding the index of the first occurrence of a character in the string
int index= str1.find_first_of('e');
cout<< "The index of the first occurrence of the character e in string is : " << index;
}
The string is : Welcome
The index of the first occurrence of the character e in string is : 1
// The find_last_of() function in string class.
#include<iostream>
#include<string>
int main()
{
string str1("Welcome");
cout<< "The string is : " << str1 << "\n";
//Finding the index of the last occurrence of a character in the string
int index= str1.find_last_of('e');
cout<< "The index of the last occurrence of the character e in string is : " << index;
}
The string is : Welcome
The index of the last occurrence of the character e in string is : 6
// The capacity() function in string class.
#include<iostream>
#include<string>
int main()
{
string str1;
//Calling capacity() function to check length of an empty string.
cout<< "The capacity of first string is : " << str1.capacity() << "\n";
string str2("Welcome");
cout<<"The capacity of second string is : " << str2.capacity();
}
The capacity of first string is : 15
The capacity of second string is : 15
// The capacity() function in string class.
#include<iostream>
#include<string>
int main()
{
string str1;
//Calling capacity() function to check capacity of an empty string.
cout<< "The capacity of first string is : " << str1.capacity() << "\n";
string str2("Welcome");
//Calling capacity() function to check capacity of an non-empty string.
cout<<"The capacity of second string is : " << str2.capacity();
}
The capacity of first string is : 15
The capacity of second string is : 15
// The empty() function in string class.
#include<iostream>
#include<string>
int main()
{
string str1;
//Calling empty() function to check if the string is empty or not
cout<< "The length of first string is : " << str1.empty() << "\n";
string str2("Welcome");
//Calling empty() function to check if the string is empty or not
cout<<"The length of second string is : " << str2.empty();
}
The length of first string is : 1
The length of second string is : 0
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement