String in CPP
String in CPP
Topperworld.in
String in C++
The syntax to create a string in C++ is quite simple. We use the string keyword
to create a string in C++. However, we also need to include the standard string
class in our program before using this keyword.
Syntax:
There are two ways in which we can save/ represent the strings in C++,
namely:
1. C- style string
2. String Class
©Topperworld
C++ Programming
Syntax:
char str_array[7] = {‘D’, ‘e’, ‘e’, ‘p’, ‘a’, ‘k’, ‘\0’};
The char str_array[] will still include 6 characters due to the fact that C++
automatically adds a null character at the end of the string.
String Class
Syntax:
std::string str("Topperworld");
In the case of strings, memory The size of the character array has
is allocated dynamically. More to be allocated statically, more
©Topperworld
C++ Programming
❖ Operations on Strings
1) Input Functions
Function Definition
©Topperworld
C++ Programming
Example:
#include <iostream>
#include <string>
int main() {
std::string input;
return 0;
}
Output:
2) Capacity Functions
Function Definition
©Topperworld
C++ Programming
Function Definition
This function changes the size of the string, the size can be
resize()
increased or decreased.
Example:
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers;
std::cout << "Initial capacity: " <<
numbers.capacity() << std::endl;
// Adding elements to the vector
for (int i = 0; i < 10; ++i) {
numbers.push_back(i);
std::cout << "Size: " << numbers.size() << ",
Capacity: " << numbers.capacity() << std::endl;
}
return 0;
}
©Topperworld
C++ Programming
Output:
Initial capacity: 0
Size: 1, Capacity: 1
Size: 2, Capacity: 2
Size: 3, Capacity: 4
Size: 4, Capacity: 4
Size: 5, Capacity: 8
Size: 6, Capacity: 8
Size: 7, Capacity: 8
Size: 8, Capacity: 8
Size: 9, Capacity: 16
Size: 10, Capacity: 16
3) Iterator Functions
Function Definition
©Topperworld
C++ Programming
Function Definition
Algorithm:
• Declare a string
• Try to iterate the string using all types of iterators
• Try modification of the element of the string.
• Display all the iterations.
Example:
#include <iostream>
#include <vector>
int main() {
int numbers[] = {1, 2, 3, 4, 5};
std::vector<int> vec = {10, 20, 30, 40, 50};
std::cout << "Array: ";
©Topperworld
C++ Programming
Output:
Array: 1 2 3 4 5
Vector: 10 20 30 40 50
4) Manipulating Functions
Function Definition
©Topperworld
C++ Programming
Example:
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> source = {1, 2, 3, 4, 5};
std::vector<int> destination(source.size());
std::copy(source.begin(), source.end(),
destination.begin());
std::vector<int> first = {10, 20, 30};
std::vector<int> second = {100, 200, 300};
first.swap(second);
std::cout << "Copied: ";
for (int num : destination) std::cout << num << " ";
std::cout << "\nSwapped: ";
for (int num : first) std::cout << num << " ";
std::cout << "| ";
for (int num : second) std::cout << num << " ";
return 0;
}
Output:
Copied: 1 2 3 4 5
Swapped: 100 200 300 | 10 20 30
©Topperworld