0% found this document useful (0 votes)
0 views17 pages

Oopm Notes 1

C++ strings can be represented using C-style character strings or the String class (std::string), which allows for easy manipulation of text. The String class includes various built-in functions such as find(), append(), insert(), erase(), replace(), and compare() to manage string data. Examples demonstrate how to use these functions for tasks like searching for substrings, appending text, and comparing strings.

Uploaded by

gitox97530
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views17 pages

Oopm Notes 1

C++ strings can be represented using C-style character strings or the String class (std::string), which allows for easy manipulation of text. The String class includes various built-in functions such as find(), append(), insert(), erase(), replace(), and compare() to manage string data. Examples demonstrate how to use these functions for tasks like searching for substrings, appending text, and comparing strings.

Uploaded by

gitox97530
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

C++ Strings

What is a C++ String?


C++ string is a way of representing and working with texts and
characters. It has two different types of string representations:
C-style character string and String class introduced in C++.

C++ String Class


C++ is based on the OOPs concept; it enables you to represent
the string as an object of the C++ String class (std:: string). The
class allows you to declare a string variable quickly, and store
any sequence of characters in it. Here’s an example of
representing a string with the help of the String class.
#include <iostream>

using namespace std;

int main(){

string greet = "Hello";

cout << "This is a greeting message: ";

cout<<greet<<endl;

}
C++ String Built-In Functions
Function Description

Used to read and store a string that the


getline()
user enters through the input stream

Inputs a new character to the string’s


push_back()
end

Pops out (deletes) a string’s last


pop_back()
character (introduced from C++11)
#include<iostream> #include<string>
using namespace std;
int main(){
string s;
cout << "Enter a string: "; getline(cin,s);
cout << "This is initial string : "; cout << s << endl;
s.push_back('i');
cout << "The new string is : "; cout << s << endl;
s.pop_back();
cout << "After pop_back operation, the string is : ";
cout << s << endl; return 0;
}
String Class Functions
The common functions that are used with the string class are.

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.

erase() Erase characters from the string.

replace() Replace portions of the string.


compare() Compare two strings.
• Example 1: Find a Substring in the Given String
The find() function searches the string for the first occurrence of
the specified substring and returns the position of the first
character of the first match.

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; }

You might also like