Strings in C++

Strings in C++

In C++, a string is a sequence of characters used to represent text. Strings are an essential part of programming as they allow us to work with textual data efficiently. Whether you’re dealing with user inputs, file manipulation, or any other text-based operations, understanding how to use strings in C++ is crucial.

strings in C++

Declaration,access and initialization of String in C++

A string is defined as an array of characterswith additional predefined methods support through headers
std::string ="value";
ex:
std::string name ="trishaank";//direct declaration and initialization
cout<<name;//directly access by string name
String in c++

Advantages of Using Strings in C++:

Initialising at runtime

Run

#include 
using namespace std; 
int main()
{
string name;
cout<<"enter a name:"; cin>>name;
cout<<endl;
cout<<"given string is "<<name;
return 0;
}

Output:

enter a name:Anurag Sharma 

given string is Anurag

In the above example, we can see that the string is breaking after white space.to overcome this we use getline() method

getline()

  • It is used to accept a string of multiple words i.e it considers whitespace also a character in it
  • getline() function takes two parameters.
    The first one is std::cin and the second one is the name of our string variable.
						
Run
#include <iostream>
using namespace std;
int main ()
{
  string name;
  cout << "\nenter a name:";
  getline (cin, name);		//reading a string
  cout << "\ngiven string is " << name;
  return 0;
}

Output:

enter a name:Anurag Sharma 

given string is Anurag Sharma 

Concatenation operator(+)

We can directly  join the strings using ‘+’ operator, hence it as an overloaded operator

Run
#include <iostream>
using namespace std;
int main(){
string s1="trish";
string s2="rish";
cout<<s1+s2<<endl;//trishrish
return 0;
}

Output

trishrish

length()

length() function returns the length of the string including whitespace characters.

Run
#include <iostream>
using namespace std;
int main()
{
string name; name = "My name is 3shaank";
cout << name.length() <<endl;//includes whitespaces also
return 0;
}

Output

18

Printing only characters in between

Run
#include  <iostream>
using namespace std; 
int main() 
{ 
string str1 ("trishank"); 
cout << str1.substr(1,4) << endl;//charecters from 1 to 4 
return 0; 
}

Output

rish
Here, 4 characters starting from position 1 got printed.

find()

find() function finds the position of the first occurrence of a character
Run
#include <iostream>
 using namespace std;
 int main()
{ 
string str1 ("3sh");
cout << str1.find('s') << endl;//index of 's' 
return 0; 
}

Output

1

In this example, find() returned the position of ‘s’ in the string str1.

Find the position of a string in another string.

Run
#include <iostream>
using namespace std;
int main()
{
string str1 ("trishaank kumar");
string str2 ("kumar");
cout <<
str1.find(str2) <<endl; //start index of str2 in str 1
return 0;
}
Output
11

compare()

compare() function compares the value of a string ( str1 ) with another string ( str2 ).
  1. It returns 0 if both the strings are equal.
  2. It returns a positive value if either str1 > str2 or the first unmatched letter of str1 is greater than that of str2.
  3. It returns a negative value if either str1 < str2 or the first unmatched letter of str1 is less than that of str2.

clear()

This function clears the data present strings and makes it empty.

Run
#include <iostream>
using namespace std;
int main()
{
string s = "I hate my college";
s.clear();//string became empty
cout << "Value of s is :" << s << endl;
cout<<"size of s"<<s.length();// 0 because empty
return 0;
}

Output

Value of s  is :
size :0 

Example 1

Run
# include <iostream>
using namespace std;
int main()
{
string str1 ("trishaank");
string str2 ("trish");
cout << str1.compare(str2) << endl;
return 0;
}

Output:

4

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription