C++ String Functions
C++ provides some inbuilt functions which are used for string manipulation, such as the strcpy() and strcat() functions for copying and concatenating strings. Some of them are:
Function |
Description |
---|---|
length() | This function returns the length of the string. |
swap() | This function is used to swap the values of 2 strings. |
size() | Used to find the size of string |
resize() | This function is used to resize the length of the string up to the given number of characters. |
find() | Used to find the string which is passed in parameters |
push_back() | This function is used to push the passed character at the end of the string |
pop_back() | This function is used to pop the last character from the string |
clear() | This function is used to remove all the elements of the string. |
strncmp() | This function compares at most the first num bytes of both passed strings. |
strncpy() | This function is similar to strcpy() function, except that at most n bytes of src are copied |
strrchr() | This function locates the last occurrence of a character in the string. |
strcat() | This function appends a copy of the source string to the end of the destination string |
find() | This function is used to search for a certain substring inside a string and returns the position of the first character of the substring. |
replace() | This function is used to replace each element in the range [first, last) that is equal to old value with new value. |
substr() | This function is used to create a substring from a given string. |
compare() | This function is used to compare two strings and returns the result in the form of an integer. |
erase() | This function is used to remove a certain part of a string. |
C++ Strings iterator functions
In C++ inbuilt string iterator functions provide the programmer with an easy way to modify and traverse string elements. These functions are:
Functions | Description |
---|---|
begin() | This function returns an iterator pointing to the beginning of the string. |
end() | This function returns an iterator that points to the end of the string. |
rfind() | This function is used to find the string’s last occurrence. |
rbegin() | This function returns a reverse iterator pointing to the end of the string. |
rend() | This function returns a reverse iterator pointing to the beginning of the string. |
cbegin() | This function returns a const_iterator pointing to the beginning of the string. |
cend() | This function returns a const_iterator pointing to the end of the string. |
crbegin() | This function returns a const_reverse_iterator pointing to the end of the string. |
crend() | This function returns a const_reverse_iterator pointing to the beginning of the string. |
Example:
C++
// C++ Program to demonstrate string iterator functions #include <iostream> using namespace std; int main() { // declaring an iterator string::iterator itr; // declaring a reverse iterator string::reverse_iterator rit; string s = "w3wiki" ; itr = s.begin(); cout << "Pointing to the start of the string: " << *itr<< endl; itr = s.end() - 1; cout << "Pointing to the end of the string: " << *itr << endl; rit = s.rbegin(); cout << "Pointing to the last character of the string: " << *rit << endl; rit = s.rend() - 1; cout << "Pointing to the first character of the string: " << *rit << endl; return 0; } |
Pointing to the start of the string: G Pointing to the end of the string: s Pointing to the last character of the string: s Pointing to the first character of the string: G
String Capacity Functions
In C++, string capacity functions are used to manage string size and capacity. Primary functions of capacity include:
Function | Description |
---|---|
length() | This function is used to return the size of the string |
capacity() | This function returns the capacity which is allocated to the string by the compiler |
resize() | This function allows us to increase or decrease the string size |
shrink_to_fit() | This function decreases the capacity and makes it equal to the minimum. |
Example:
C++
#include <iostream> using namespace std; int main() { string s = "w3wiki" ; // length function is used to print the length of the string cout << "The length of the string is " << s.length() << endl; // capacity function is used to print the capacity of the string cout << "The capacity of string is " << s.capacity()<< endl; // the string.resize() function is used to resize the string to 10 characters s.resize(10); cout << "The string after using resize function is " << s << endl; s.resize(20); cout << "The capacity of string before using shrink_to_fit function is " << s.capacity() << endl; // shrink to fit function is used to reduce the capacity of the container s.shrink_to_fit(); cout << "The capacity of string after using shrink_to_fit function is " << s.capacity() << endl; return 0; } |
The length of the string is 13 The capacity of string is 15 The string after using resize function is GeeksforGe The capacity of string before using shrink_to_fit function is 30 The capacity of string...
In conclusion, this article explains how strings can be defied in C++ using character arrays and string classes. The string class provides more advanced features, while the character array provides basic features but is efficient and easy to use. In this article, we also discussed the various methods to take input from the user.
To know more about std::string class, refer to the article – std::string class in C++
Strings in C++
C++ strings are sequences of characters stored in a char array. Strings are used to store words and text. They are also used to store data, such as numbers and other types of information. Strings in C++ can be defined either using the std::string class or the C-style character arrays.
1. C Style Strings
These strings are stored as the plain old array of characters terminated by a null character ‘\0’. They are the type of strings that C++ inherited from C language.
Syntax:
char str[] = "w3wiki";
Example:
C++
// C++ Program to demonstrate strings #include <iostream> using namespace std; int main() { char s[] = "w3wiki" ; cout << s << endl; return 0; } |
w3wiki
2. std::string Class
These are the new types of strings that are introduced in C++ as std::string class defined inside <string> header file. This provides many advantages over conventional C-style strings such as dynamic size, member functions, etc.
Syntax:
std::string str("w3wiki");
Example:
C++
// C++ program to create std::string objects #include <iostream> using namespace std; int main() { string str( "w3wiki" ); cout << str; return 0; } |
w3wiki
One more way we can make strings that have the same character repeating again and again.
Syntax:
std::string str(number,character);
Example:
C++
#include <iostream> using namespace std; int main() { string str(5, 'g' ); cout << str; return 0; } |
Output:
ggggg
Contact Us