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

String Fxns

The document provides an overview of various built-in string functions in C++, including append, find, swap, size, erase, push_back, pop_back, clear, and replace. Each function is defined with its syntax and includes example code snippets demonstrating its usage. The document serves as a reference for understanding how to manipulate strings in C++.

Uploaded by

k.t.kanishaa13
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 views26 pages

String Fxns

The document provides an overview of various built-in string functions in C++, including append, find, swap, size, erase, push_back, pop_back, clear, and replace. Each function is defined with its syntax and includes example code snippets demonstrating its usage. The document serves as a reference for understanding how to manipulate strings in C++.

Uploaded by

k.t.kanishaa13
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/ 26

String Built-in

Function
Here is where your presentation begins
append()

•Definition: Adds more characters to the end of the string.

•Syntax:string.append("additional_string");
Eg;
● std::string str = "Hello";
● str.append(" World");

● // Output: "Hello World"


find()

•Definition: Finds the first occurrence of a substring or character


and returns its index.

•Syntax:string.find("substring_to_find");
Eg;

● std::string str = "Hello, World";


● size_t index = str.find("World");

// Output: 7
swap()
• This function is used to swap the values of 2 string
int main()
{
char str1[10] = "geeks";
char str2[10] = "forgeeks";
swap2(str1, str2);
printf("str1 is %s, str2 is %s", str1, str2);
getchar();
return 0;
}
The syntax of swap() is swap(string1, string2);
size()
• size() function is used to return the length of the string in terms
of bytes.
• It defines the actual number of bytes that conform to the
contents of the String object.

• Syntax: str.size()
.

#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";

std::cout << "The size of the string is: " << str.size() << std::endl;
return 0;
}
erase()

•Definition: Removes a portion of the string starting at a given


position and for a specified length.

•Syntax: string.erase(start_pos, length);


.

● std::string str = "Hello, World!";


● str.erase(5, 7);

// Output: "Hello"
push_back()

● This function is used to push the passed


character at the end of the string

● Syntax: vector_name.push_back(value);
.

#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers;
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
for (int i = 0; i < numbers.size(); ++i) {
std::cout << numbers[i] << " ";
}

return 0;
}

-----------------------------------------------------------------------------------------------------------------------------

10 20 30
pop_back()

● This function is used to pop the last


character from the string

● Syntax: vector_name.pop_back();
#include <iostream>
.

#include <vector>
int main() {
std::vector<int> numbers = {10, 20, 30};
numbers.pop_back();
for (int i = 0; i < numbers.size(); ++i) {
std::cout << numbers[i] << " ";
}

return 0;
}

--------------------------------------------------------------------------------------------------------------------------

10 20
clear()

● This function is used to remove all the elements of


the string

● Syntax: container_name.clear();
.

#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {10, 20, 30, 40, 50};
numbers.clear();
if (numbers.empty()) {
std::cout << "The vector is empty!" << std::endl;
} else {
for (int i = 0; i < numbers.size(); ++i) {
std::cout << numbers[i] << " ";
}
}

return 0;
}

---------------------------------------------------------------------------------------------------------------------------
The vector is empty!
replace()

● This function is used to replace each element in the


range [first, last) that is equal to old value with new
value.

Syntax: string_name.replace(start_pos, length, new_string);


.

#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
str.replace(7, 5, "C++");
std::cout << str << std::endl;

return 0;
}

----------------------------------------------------------------------------------------------------------------------------

● Hello, C++!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy