C++ Tutorials

C++ Tutorials

Strings in C++ [Complete Tutorial]: Basic Input Output with Spaces, Joint Multiple Strings, Access, Clear, Find, Insert, Delete, Sort, Compare, Convert integer to string and string to integer, Particular Character Access, Check String Equal or not, Check Empty String, Check Size Using Strings, Check SubString

Basic Strings Input and Output in C++

#include<iostream>
#include<string>
using namespace std;

int main(){

string str;
cout<<"Enter any text: ";
cin>>str;
cout<<"Your typing text: "<<str;


return 0;
}

Print n time any character using Strings in C++

#include<iostream>
#include<string>
using namespace std;

int main(){


string str (5, 'p');
cout<<str<<endl;

return 0;
}

Input Output with spaces Using String in C++

#include<iostream>
#include<string>
using namespace std;

int main(){

string str;
//Print with spaces in string
cout<<"Enter any text: ";
getline(cin, str);

cout<<"Your typing text: "<<str;

return 0;
}

Joint Multiple Strings in C++

#include<iostream>
#include<string>
using namespace std;

int main(){
string s1="Ind";
string s2="ia";
//s1.append(s2);
cout<<"Your Joint text: "<<s1+s2;

return 0;
}
//Or
#include<iostream>
#include<string>
using namespace std;

int main(){

string s1="Ind";
string s2="ia";
s1.append(s2);
cout<<"Your Joint text: "<<s1;

return 0;
}

Particular Character Access in String Using C++

#include<iostream>
#include<string>
using namespace std;

int main(){
string s1="Hundu";
string s2="Stan";

cout<<"Your Find Char: "<<s1[2];

return 0;
}

Clear String in C++

#include<iostream>
#include<string>
using namespace std;

int main(){
string abc="Hi, Welcome to my Website";
//Clear string
abc.clear();
cout<<"Your Text Clear: "<<abc;

return 0;
}

Compare Strings in C++

#include<iostream>
#include<string>
using namespace std;

int main(){

string first="abc";
string sceond="xyz";
cout<<"Your Compare Text: "<<first.compare(sceond);

return 0;
}

Check String Equal or not in C++

#include<iostream>
#include<string>
using namespace std;

int main(){

string first;
string sceond;
cout<<"Enter first string: ";
cin>>first;
cout<<"Enter sceond string: ";
cin>>sceond;
if(first.compare(sceond)==0){
  cout<<"Strings are equal";
}
else{
  cout<<"String are not equal";
}
return 0;
}

Check Empty String in C++

#include<iostream>
#include<string>
using namespace std;

int main(){
string check="Hello World";
cout<<check<<endl;
//Clear string
check.clear();
if(check.empty()){
  cout<<"String is empty";
}
else if(!check.empty()){
  cout<<"String is not empty";
}
return 0;
}

Erase/Remove/Delete String in C++

#include<iostream>
#include<string>
using namespace std;

int main(){
string del="Coooding";
del.erase(1, 2);
cout<<"After Erase text: "<<del;
return 0;
}

Find Strings in C++

#include<iostream>
#include<string>
using namespace std;

int main(){
string fnd="Cooccoding";
cout<<"Your Find Text: "<<fnd.find("cod");
return 0;
}

Insert Strings in C++

#include<iostream>
#include<string>
using namespace std;

int main(){
string insrt="Cong";
insrt.insert(2, "di");
cout<<"Insert Strings: "<<insrt;

return 0;
}

Check Size Using Strings in C++

#include<iostream>
#include<string>
using namespace std;

int main(){
string sze="Coding";

cout<<"Check Size Strings: "<<sze.size();

return 0;
}

//Another Option
#include<iostream>
#include<string>
using namespace std;

int main(){
string sze="Coding";

cout<<"Check Size Strings: "<<sze.length();

return 0;
}

Substring a String in C++

#include<iostream>
#include<string>
using namespace std;

int main(){
string sub_s="Codings";

string s=sub_s.substr(2, 4);
cout<<s<<endl;

return 0;
}

Converting Strings to Integer/Numbers in C++

#include<iostream>
#include<string>
using namespace std;

int main(){
//Numeric String

string num_S="91";
//Convert String -->int 
int x= stoi(num_S);
cout<<x+5<<endl;


return 0;
}

Converting Integer/Numbers To Strings in C++

#include<iostream>
#include<string>
using namespace std;

int main(){
//Numeric String

int x=91;
//Convert int --> String

cout<<to_string(x)+"9"<<endl;


return 0;
}

Sort string c++

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main(){
string srt="cbdafeihg";
sort(srt.begin(), srt.end());
cout<<srt;

return 0;
}

Strings in C++

Hi we will learn about strings in C++. C++ comes with a library for string which comes with very important and useful functions. Let’s learn about them in this tutorial. C++ string class internally uses char array to store character but all memory management, allocation, and null termination is handled by string class itself that is why it is easy to use.

The length of c++ string can be changed at runtime because of dynamic allocation We will be using the library bits/stdc++.h from here on as it includes all the libraries in C++. To declare a string variable we write string followed by the variable name.

Just like, normal variables. To initialize a string we write the string in double quotes inside parenthesis of the string. Like this. Let’s print this string. We just need to write cout to print this string. We can see that the output is as expected. To initialize a string with multiple occurance of a character or substring,

we write the number of occurrences we want then a comma followed by the substring in quotes. Here, we are printing a string with 5 stars. Let’s run this code, we can see that the output is as expected. We can find the length of string using .length() function. To use this, we just need to write variable name dot length() like this. To find the character at some index we use the .at function, here we are finding the value at index 4. To clear a string, we write variable name.clear(). Let’s run this code, we can see that the output is as expected. To append a substring to a string we use .append function. Like, here in this case we are appending forgeeks to s1. The substr function prints the substring of length b starting from index a.

If we do not pass b in the function, it automatically takes this value as length-1. Let’s run this code, we can see that s2 is appended to s1. And the substring Geeks is printed. To delete specific part of a string we use the erase the function. This function deletes a string of length b starting from the index a. Here, we are deleting a substring of length 3 from index 3. Let’s run this code,

we can see that the output is as expected. To find a pattern or substring in a string we use the find function, this returns the index at which the substring is found, else it returns 0. Like, here we are finding “for” in “GeeksforGeeks”. To replace a substring of a string, we use .replace function. This function replaces b characters from a index with “str”. Here, we are replacing for with “are”. Let’s run this code, we can see that for is found at the index 5 and it is replaced with are.