Strings in C++

Strings in C++

  • Like primitive datatypes, C++ has built-in support for String under a header file  called #include<string>
  • This  header has rich built-in support of strings, this header is present in thestd namespace like cout and cin

A string is defined as an array of characters with additional predefined methods support through headers

Declaration,access and initialization of String

std::string ="value";
ex:
std::string name ="trishaank";//direct declaration and initialization
cout<<name;//directly access by string name
String Class in C++

Initialising at runtime

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

main()
{
string s1;
cout<<"enter a name:";
cin>>name;
cout<<"given string is"<<name;
}

O/P:

enter a name
trishaank rishvik
given string is trishaank

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.
main()
{
string name;
cout<<"\nenter a name:";
getline(cin,name);//reading a string
cout<<"\ngiven string is "<<name;
}

O/P

enter a name
3shaank
given string is 3shaank

Concatenation operator(+)

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

main()
{
string s1="trish";
string s2="rish";
cout<<s1+s2<<endl;//trishrish
}

O/P

trishrish

length()

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

main()
{
string name;
name = "My name is 3shaank";
cout << name.length() <<endl;//includes whitespaces also
}

Output

18

empty()

  • This function returns whether a string is empty or not.
  • It returns 1 if the string length is 0 and 0 if not.
main()
{
string s1, s2;
s1 = "";// empty string
s2 = "trishaank";//non_empty
cout << s1.empty() << endl;//returns 1 because empty string
cout << s2.empty() << endl;//returns 0 because empty string
}

Output

1
0

resize()

  • resize() function resizes our string to a specified length and adds another character if required
  • Suppose, the specified length is 5. If the specified length (i.e. 5 ) is smaller than the current length ( suppose 7 ),  then the string will contain only the first 5 characters.
  • If the specified length ( 5 ) is greater than the current length ( suppose 3 ), then the string will contain 5 characters ( 3 of the original string and two other specified characters ).
  • It will accept only one character in the function
main()
{
string s = "trishaank";
int size = s.size(); // size of s
s.resize( size+1, 'k' ); // resizing 
cout << s << endl;
}

Output

trishaank k

clear()

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

main()
{
string s = "I hate my college";
s.clear();//string became empty
cout << "Value of s is :" << s << endl;
cout<<"size of s"<<s.lenght();// 0 because empty
}

Output

Value of s  is :
size :0

‘[]’ operator

It returns the character at some specified position in a string.

main()
{
string s = "3shaank";
cout << "Character at 2nd position : " << s[2] << endl;//index 2
}

Output

Character at 2nd position : h

substr()

substr() function returns a substring from a string by specifying its position.

main()
{
string str1 ("trishaank");
cout << str1.substr(3) << endl;//start printing from index 3 's'
}

Output

shaank

The above example printed all the characters from position 3 till the end of the string str1.

Printing only charecters in between

main()
{
string str1 ("trishank");
cout << str1.substr(1,4) << endl;//charecters from 1 to 4
}

Output

rish

Here, 4 characters starting from position 1 got printed.

find()

find() function finds the position of the first occurrence of a character

main()
{
string str1 ("3sh");
cout << str1.find('s') << endl;//index of 's'
}

Output

1

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

Find the position of a string in another string.

main()
{
string str1 ("trishaank kumar");
string str2 ("kumar");
cout << str1.find(str2) <<endl; //start index of str2 in str 1
}

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.

Example 1

main()
{
string str1 ("trishaank");
string str2 ("trishaanksantosh");
cout << str1.compare(str2) << endl;
}

Output

10

Example2

main()
{
string str1 ("trishaank");
string str2 ("trish");
cout << str1.compare(str2) << endl;
}

Output

6