Oopm Notes 1
Oopm Notes 1
int main(){
cout<<greet<<endl;
}
C++ String Built-In Functions
Function Description
Function Description
find() Find a substring in the string.
Find the last occurrence of a substring in the
rfind()
string.
append() Append to the string.
insert() Insert into the string.
The rfind() function searches the string for the last occurrence
of the specified substring and returns the position of the first
character of the last match.
#include <iostream>
#include <string>
using namespace std;
int main()
{ string str = "Hello world, wonderful world!";
cout << "String: " << str << endl;
size_t first_occurrence = str.find("world");
size_t last_occurrence = str.rfind("world");
if (first_occurrence != string::npos)
{ cout << "First occurrence: 'world' found at position: “
<< first_occurrence << endl;
cout << "Last occurrence: 'world' found at position: "
<< last_occurrence << endl;
} else
{ cout << "'world' not found" << endl; }
return 0; }
Example 2: Append to a String
We can append a string to an existing string using
the append() function. For example,
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello world,";
cout << "Before: " << str << endl;
str.append(" Have a good day!");
cout << "After: " << str << endl;
return 0; }
Insert a String at a Given Position
• We can insert a string at any given position
using the insert() function. For example,
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello world, world!";
cout << "Before: " << str << endl;
str.insert(13, " beautiful");
cout << "After: " << str << endl;
return 0; }
Erase N Characters From the Given
Position
#include <iostream>
#include <string>
using namespace std;
int main()
{ string str = "Hello world, beautiful world!";
cout << "Before: " << str << endl;
index str.erase(7, 5);
cout << "After: " << str << endl; return 0; }
Replace N Characters Within A String
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello world, beautiful world!"; cout
<< "Before: " << str << endl;
str.replace(6, 2, "Earth");
cout << "After: " << str << endl;
return 0; }
Compare Strings Alphabetically
The compare() function in the C++ string class
returns an integer that indicates the
lexicographical relationship between the
compared strings. It returns:
0 - if the compared strings are equal.
< 0 - if the calling string is lexicographically less
than the compared string.
> 0 - if the calling string is lexicographically
greater than the compared string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "Hello world";
string str2 = "Hello world";
string str3 = "Hello";
string str4 = "Hello world, What's Up?";
cout << "String 1: " << str1 << endl;
cout << "String 2: " << str2 << endl;
cout << "String 3: " << str3 << endl;
cout << "String 4: " << str4 << endl;
cout <<"Comparing String 1 and String 2: " << str1.compare(str2) << "
(Equal)" <<endl;
cout <<"Comparing String 1 and String 3: " << str1.compare(str3) << "
(String 1 is Longer)" << endl; cout <<"Comparing String 1 and String 4:
" << str1.compare(str4) <<" (String 1 is Smaller)" << endl;
return 0; }