Home >>C++ Tutorial >C++ Strings

C++ Strings

C++ Strings

In order to represent the sequence of characters, an object of std::string is used, that is known as the strings in C++.Operations like, conversion and comparison can be performed on these strings in the C++ language.

C++ Strings Functions

Function Description
int length() This function is used to find the length of the string.
int compare(const string& str) This function is used to compare two string objects.
int size() This function is used to return the length of the string generally in terms of bytes.
void resize(int n) This function is used to resize the length of the string up to n characters.
string& replace(int pos,int len,string& str) This function is used to replace portion of the string that begins at character position pos and spans len characters.
void swap(string& str) This function is used to swap the values of two string objects.
char& at(int pos) This function is used to access an individual character at specified position pos.
int find(string& str,int pos,int n) This function is used to find the string specified in the parameter.
int find_first_of(string& str,int pos,int n) This function is used to find the first occurrence of the specified sequence.
int find_first_not_of(string& str,int pos,int n ) This function is used to search the string for the first character that does not match with any of the characters specified in the string.
int find_last_of(string& str,int pos,int n) This function is used to search the string for the last character of specified sequence.
int find_last_not_of(string& str,int pos) This function generally searches for the last character that does not match with the specified sequence.
string& insert() This function generally inserts a new character before the character indicated by the position pos.
int max_size() This function is used to find the maximum length of the string.
void push_back(char ch) This function is used to add a new character ch at the end of the string.
void pop_back() This function is used to remove a last character of the string.
string& assign() This function is used to assign new value to the string.
int copy(string& str) This function is used to copy the contents of string into another.
char& back() This function is used to return the reference of last character.
Iterator begin() This function is used to return the reference of first character.
int capacity() This function is used to return the allocated space for the string.
const_iterator cbegin() This function is used to point to the first element of the string.
const_iterator cend() This function is used to point to the last element of the string.
void clear() This function is used to remove all the elements from the string.
const_reverse_iterator crbegin() This function is used to point to the last character of the string.
char& front() This function is used to return a reference of the first character
bool empty() This function is used to check whether the string is empty or not.
string& erase() This function is used to remove the characters as specified.
string& operator+=() This function is used to append a new character at the end of the string.
string& operator=() This function is used to assign a new value to the string.
char operator[](pos) This function is used to retrieve a character at specified position pos.
int rfind() This function is used to searche for the last occurrence of the string.
iterator end() This function is used to make references the last character of the string.
reverse_iterator rend() This function is used to point to the first character of the string.
void shrink_to_fit() This function is used to reduce the capacity and makes it equal to the size of the string.
char* c_str() This function is used to return pointer to an array that contains null terminated sequence of characters.
const_reverse_iterator crend() This function is used to reference the first character of the string.
void reserve(inr len) This function is used to request a change in capacity.
reverse_iterator rbegin() This function is used to reference the last character of the string.
allocator_type get_allocator(); This function is used to return the allocated object associated with the string.

Here is an example of the C++ strings:

#include <iostream>  
using namespace std;  
int main( ) 
{  
    string str = "Welcome";    
	cout<<str<<endl;        
}  
Output :
Welcome

C++ String Concat Example

Here is an example of the strings that uses the strcat() function:

#include <iostream>  
#include <cstring>  
using namespace std;  
int main()  
{  
    char str[10], str1[10];  
    cout << "Enter the First string: ";  
    cin.getline(str, 10);  
    cout << "Enter the Second string: ";  
     cin.getline(str1,10);  
    strcat(str,str1);   
    cout << "Str = " << str << endl;  
    cout << "Str1 = " << str1<<endl;  
    return 0;  
}  
Output :
Enter the First string: hello
Enter the Second string: user
Str = hello user
Str1 = user

C++ String Copy Example

Here is an example of the strings that uses strcpy() function:

#include <iostream>  
#include <cstring>  
using namespace std;  
int main()  
{  
    char str[10], str1[10];  
    cout << "Enter the First string: ";  
    cin.getline(str, 10);  
    strcpy(str1,str);  
    cout << "First String = "<< str << endl;  
    cout << "Second String = "<<str1<<endl;  
    return 0;  
}  
Output :
Enter the First string: Welcome
First String = Welcome
Second String = Welcome

C++ String Length Example

Here is an example of the strings that uses strlen() function in C++:

#include <iostream>  
#include <cstring>  
using namespace std;  
int main()  
{  
    char str[] = "Welcome to the world of C++ Programming";  
    cout << "String Length= " << strlen(str)<<endl;  
    return 0;  
}  
Output :
String Length= 39

No Sidebar ads