AppDividend
Latest Code Tutorials

C++ String Class Tutorial | String Class in C++ Example

0

C++ String Class Tutorial | String Class in C++ Example, is today’s topic. C++ being object-oriented, allows us to declare a series of characters using an object of inbuilt “String” class. This class is referred to as std:: string. This class provides us the features to store the characters as a series of bytes which makes us access a single byte of the character taken into account.

C++ String Class

String class stores the characters as a sequence of bytes with the functionality of allowing access to a single byte character. If we want to use the String Class, <string>(String Header) has to include necessarily.

#Declare and initialize a String

String str;
str = ”Hello World”;

Here String is the name of Class, and the str is the object of class String.

#Difference Between String And Character Array

          

          Character Array

               String

1. Memory is allocated statically. So there is wastage of memory. As allocated at compile-time. 

1. Memory is allocated dynamically So there is no wastage of memory as allocated at run-time.

2. Operators in C++ can’t be applied on character array.

  

2. Standard C++ Operator can be applied on String.

3. In the case of the character, array Implementation is faster.     

3. In the case of Strings, there is Slower implementation.

4. char array-name [size]; 4. string string-name;

5. Character array is a collection of variables of the character data type.

5. The string is class, and variables of the string are the objects of class “string.”

 

#Operations on String

Let’s see the different operations of String in C++.

#Length of the String

The length( ) uses to calculate the number of characters in that string.

int len;
string str = "Hello World";
len=str.length(); 
cout<<len;

This len stores the length of string str and output will be the length of the string str.

See the following output.

11

#Compare two strings

Values in two strings can be checked for equality using compare( ) function  Or “==.”

string str1 = "App";
string str2 = "Dividend";
 if (str1 == str2) //or we can use  if (str1.compare(str2) == 0)
      cout << "Equal";
  else
      cout << "Not Equal";

See the following output.

Not Equal

#Add a character at the end

The push_back( ) function is used to insert a character at the end of the string. It requires only one, the string or character you want to insert.

str.push_back('a');

It will add ‘a’ character at the end of the str.

#Delete a character at the end

The pop_back( ) function is used to delete a character at the end of the string.

str.pop_back(); 

#Change the size of the string

The resize( ) function is used to increase or decrease a size of the string str. The parameter you need to give is the new size of the string.

str.resize(string_size);

See the following example.

str.resize(13);

Makes the size of the string str 13.

#Appending Two Strings

Two string can be appended by using the ‘+’ or the append( ) function.

string str1 = "App";
string str2 = "Dividend";
string str3 = str1 +" "+ str2; //or we can use str1.append(str2);

The output of str3 will be App Dividend

#Swapping of two strings

Two strings can be swapped by using swap( ) function. See the following syntax.

string1.swap(string2);

See the following example.

string str1 = "App";
string str2 = "Dividend";
str1.swap(str2); 

#Reversing the iterator

  1. rbegin( ): used to return a reverse iterator pointing at the end of the string.
  2. rend( ): used to return a reverse iterator pointing at the beginning of the string.

Example:

for (st=str.rbegin(); st!=str.rend(); st++) 
{
     cout<<st;
}

#Know the capacity

The capacity( ) function is used to know the capacity taken by the string. It may or may not be equal to the size of the string. See the following syntax.

string_name.capacity();

#Using the substring

This function is used to store substring of a string.

See the following syntax.

substr (start index,end index);

This function takes two parameters,

Here start index is the index from where we want to start substring and end index is the last of the substring.

int main()
{
       string str= "AppDividend";
       string sub= str.substr(1,4);
       cout << "String: " << sub;
}

See the output.

ppDi

#Replacing the content

There is a function called assign() in C++ used to assign a new value to string, and its current value has been replaced.

See the following syntax.

old_string.assign(new_string);

See the following code example.

int main( )
{
      str1="App";
      str2="Dividend"
      str1.assign(str2);
      cout<<str1;
}

This will make str1 be Dividend. So in this way we have replaced the content of str1. See the following output.

Dividend

#Maximum Size of String

The max_size( ) tells us the maximum size that the string can reach.

See the following syntax.

string_name.max_size();

It is the maximum potential length.

#Erase the content of the String

The clear( ) function will erase all the content stored in the string. See the following syntax.

string_name.clear( );

#Shrink_to_fit

It helps to reduce the capacity of the string to fit its size without affecting the length of the string. See the following code example.

int main ()
{
  string str (1000,'x');
  str.resize(10);
  str.shrink_to_fit();
}

This will make the capacity 10 of the string.

#Find in the string

It searches for the first occurrence of the argument in the string. See the following syntax.

str.find(your_word);

See the following code example.

string s="Hey you, how are you";
int loc=s.find("you");
cout<<"The first occurrence of 'you' is: "<<loc;

#Get character in the string

The str.at(i) helps to get the character present at the i index of the string. See the following example.

int main ( )
{
  string str ("App Dividend");
  cout << str.at(2);
}

See the following output.

p

Hence it helps us to get the character present at the 2nd index of the string.

#Append the string

It makes the string to extend by inserting the string/characters at the end. It can be done with the help of operator +=. See the following example.

int main{
  string name ("App");
  string surname("Dividend");
  name += surname;   
  cout<<name;    
}

See the output.

AppDividend

Finally, C++ String Class Tutorial | String Class in C++ Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.