C++ String [With Examples]

In this article, we will see how to handle C++ String, declare them, initialize them, and how to use them for various input and output operations.

Introduction

C++ string is a std::string class object that represents a sequence of characters. Strings can be used for a variety of operations such as concatenation, comparison, conversion, and so on.

Code Examples

C++ String Example

Let’s see the simple example of a String.

#include <iostream>  
using namespace std;  
int main( ) {  
        string one = "Welcome to";    
        char ch[] = { 'S', 'o', 'f', 't', 'h', 'u', 'n', 't'};    
        string two = string(ch);    
        cout<<one<<endl;    
        cout<<two<<endl;    
}  

Output:

Welcome to
Softhunt

C++ String Compare Example

Let’s look at a simple string comparison using the strcmp() function.

#include <iostream>  
#include <cstring>  
using namespace std;  
int main ()  
{  
  char key[] = "Softhunt.net";  
  char buffer[50];  
  do {  
     cout<<"Which is your favourite tutorial website? ";  
     cin>>buffer;  
  } while (strcmp (key,buffer) != 0);  
 cout<<"Answer is correct!!"<<endl;  
  return 0;  
} 

Output:

Which is your favourite tutorial website? W3School
Which is your favourite tutorial website? Programmer.com
Which is your favourite tutorial website? Softhunt.net
Answer is correct!!

C++ String Concatenation Example

Let’s look at a basic string concatenation example utilizing the strcat() function.

#include <iostream>  
#include <cstring>  
using namespace std;  
int main()  
{  
    char key[25], buffer[25];  
    cout << "Enter the key string: ";  
    cin.getline(key, 25);  
    cout << "Enter the buffer string: ";  
     cin.getline(buffer, 25);  
    strcat(key, buffer);   
    cout << "Concatenation = " << key << endl;  
    return 0;  
}  

Output:

Enter the key string: Welcome to
Enter the buffer string: Softhunt.net
Concatenation = Welcome toSofthunt.net

C++ String Copy Example

Let’s look at a basic example of utilizing the strcpy() function to copy a string.

#include <iostream>  
#include <cstring>  
using namespace std;  
int main()  
{  
    char key[25], buffer[25];  
    cout << "Enter the key string: ";  
    cin.getline(key, 25);  
    strcpy(buffer, key);  
    cout << "Key = "<< key << endl;  
    cout << "Buffer = "<< buffer<<endl;  
    return 0;  
}  

Output:

Enter the key string: Welcome
Key = Welcome
Buffer = Welcome

C++ String Length Example

Let’s look at a basic example of using the strlen() function to determine the length of a string.

#include <iostream>  
#include <cstring>  
using namespace std;  
int main()  
{  
    char ary[] = "Welcome to Softhunt.net";  
    cout << "Length of String = " << strlen(ary)<<endl;  
    return 0;  
}  

Output:

Length of String = 23

C++ String to int

Let’s look at a basic example of using the stoi() function to convert a string into an int.

#include <iostream>
#include <string>

int main() {

    std::string str = "12345";
    int num;
    
    num = std::stoi(str);

    std::cout << num;

    return 0;
}

Output:

12345

C++ String find Example

This function is used to find a specific substring inside a string.

Syntax:

str1.find(str2);  
#include<iostream>  
using namespace std;  
int main()  
{  
string str= "Welcome to Softhunt.net Tutorial Website";  
cout <<  str<<'\n';  
cout <<" Position of the 'to' word is :";  
cout<< str.find("to");  
return 0;   
} 

Output:

Welcome to Softhunt.net Tutorial Website
Position of the 'to' word is :8

C++ String Functions

FunctionDescription
int compare(const string& str)Compare two string objects.
int length()Find the length of the string.
void swap(string& str)Swap the values of two string objects.
string substr(int pos,int n)It creates a new string object of n characters.
int size()Returns the length of the string in terms of bytes.
void resize(int n)Resize the length of the string up to n characters.
string& replace(int pos,int len,string& str)Replaces a portion of the string that begins at character position pos and spans len characters.
string& append(const string& str)Adds new characters at the end of another string object.
char& at(int pos)Access an individual character at a specified position pos.
int find(string& str,int pos,int n)Find the string specified in the parameter.
int find_first_of(string& str,int pos,int n)It is used to find the first occurrence of the specified sequence.
int find_first_not_of(string& str,int pos,int n )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)It is used to search the string for the last character of a specified sequence.
int find_last_not_of(string& str,int pos)Searches for the last character that does not match with the specified sequence. last character of a specified sequence.
string& insert()Inserts a new character before the character indicated by the position pos.
int max_size()Finds the maximum length of the string.
void push_back(char ch)Adds a new character ch at the end of the string.
void pop_back()Removes the last character of the string.
string& assign()Assigns a new value to the string.
int copy(string& str)Copies the contents of string into another.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. C++ Vector [With Examples]

Leave a Comment