0% found this document useful (0 votes)
8 views1 page

Strings Functions

Uploaded by

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

Strings Functions

Uploaded by

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

#include <iostream>

#include <string>
using namespace std;

int main() {
string str = "Hello, World!";

// Length of the string


cout << "Length: " << str.length() << endl;

// Accessing individual characters


cout << "First character: " << str[0] << endl;
cout << "Last character: " << str[str.length() - 1] << endl;

// Substring
cout << "Substring: " << str.substr(7, 5) << endl;

// Concatenation
string str2 = " Welcome!";
cout << "Concatenated string: " << str + str2 << endl;

// Searching
string searchStr = "World";
int found = str.find(searchStr);
if (found >=0) {
cout << "Found at index: " << found << endl;
} else {
cout << "Not found." << endl;
}

// Replacing
string replaceStr = "Universe";
str.replace(7, searchStr.length(), replaceStr);
cout << "Replaced string: " << str << endl;

// Character manipulation
for (char& c : str) {
if (islower(c)) {
c = toupper(c);
} else if (isupper(c)) {
c = tolower(c);
}
}
cout << "Case-reversed string: " << str << endl;

return 0;
}
/*
This program demonstrates some commonly used string functions:

length(): Retrieves the length of the string.


operator[]: Accesses individual characters of the string.
substr(): Extracts a substring from the string.
operator+: Concatenates two strings.
find(): Searches for a substring within the string.
replace(): Replaces a substring with another string.
islower(), toupper(), isupper(), tolower(): Manipulates the case of characters in
the string.*/

You might also like