String Fxns
String Fxns
Function
Here is where your presentation begins
append()
•Syntax:string.append("additional_string");
Eg;
● std::string str = "Hello";
● str.append(" World");
•Syntax:string.find("substring_to_find");
Eg;
// 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()
// Output: "Hello"
push_back()
● 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()
● 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()
● 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()
#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++!