18. Strings

By | September 23, 2021

C++ provides following two types of string representations −

  • The C-style character string.
  • The string class type introduced with Standard C++.

The C-Style Character String

The C-style character string originated within the C language and continues to be supported within C++. This string is actually a one-dimensional array of characters which is terminated by a null character ‘\0’. Thus a null-terminated string contains the characters that comprise the string followed by a null.

The following declaration and initialization create a string consisting of the word “Hello”. To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word “Hello.”

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

If you follow the rule of array initialization, then you can write the above statement as follows −

char greeting[] = "Hello";

Following is the memory presentation of above-defined string in C/C++ −

String Presentation in C/C++

Actually, you do not place the null character at the end of a string constant. The C++ compiler automatically places the ‘\0’ at the end of the string when it initializes the array. Let us try to print above-mentioned string −

Example

#include <iostream>
using namespace std;

int main () {
   char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
   cout << "Greeting message: ";
   cout << greeting << endl;
   return 0;
}

When the above code is compiled and executed, it produces the following result −

Greeting message: Hello

C++ String Functions

FunctionDescription
int compare(const string& str)It is used to compare two string objects.
int length()It is used to find the length of the string.
void swap(string& str)It is used to swap the values of two string objects.
string substr(int pos,int n)It creates a new string object of n characters.
int size()It returns the length of the string in terms of bytes.
void resize(int n)It is used to resize the length of the string up to n characters.
string& replace(int pos,int len,string& str)It replaces portion of the string that begins at character position pos and spans len characters.
string& append(const string& str)It adds new characters at the end of another string object.
char& at(int pos)It is used to access an individual character at specified position pos.
int find(string& str,int pos,int n)It is used to 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 )It is used to 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 specified sequence.
int find_last_not_of(string& str,int pos)It searches for the last character that does not match with the specified sequence.
string& insert()It inserts a new character before the character indicated by the position pos.
int max_size()It finds the maximum length of the string.
void push_back(char ch)It adds a new character ch at the end of the string.
void pop_back()It removes a last character of the string.
string& assign()It assigns new value to the string.
int copy(string& str)It copies the contents of string into another.
char& back()It returns the reference of last character.
Iterator begin()It returns the reference of first character.
int capacity()It returns the allocated space for the string.
const_iterator cbegin()It points to the first element of the string.
const_iterator cend()It points to the last element of the string.
void clear()It removes all the elements from the string.
const_reverse_iterator crbegin()It points to the last character of the string.
const_char* data()It copies the characters of string into an array.
bool empty()It checks whether the string is empty or not.
string& erase()It removes the characters as specified.
char& front()It returns a reference of the first character.
string&  operator+=()It appends a new character at the end of the string.
string& operator=()It assigns a new value to the string.
char operator[](pos)It retrieves a character at specified position pos.
int rfind()It searches for the last occurrence of the string.
iterator end()It references the last character of the string.
reverse_iterator rend()It points to the first character of the string.
void shrink_to_fit()It reduces the capacity and makes it equal to the size of the string.
char* c_str()It returns pointer to an array that contains null terminated sequence of characters.
const_reverse_iterator crend()It references the first character of the string.
reverse_iterator rbegin()It reference the last character of the string.
void reserve(inr len)It requests a change in capacity.
allocator_type get_allocator();It returns the allocated object associated with the string.

Following example makes use of few of the above-mentioned functions −

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

int main () {

   char str1[10] = "Hello";
   char str2[10] = "World";
   char str3[10];
   int  len ;

   // copy str1 into str3
   strcpy( str3, str1);
   cout << "strcpy( str3, str1) : " << str3 << endl;

   // concatenates str1 and str2
   strcat( str1, str2);
   cout << "strcat( str1, str2): " << str1 << endl;

   // total lenghth of str1 after concatenation
   len = strlen(str1);
   cout << "strlen(str1) : " << len << endl;

   return 0;
}

When the above code is compiled and executed, it produces result something as follows −

strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10

The String Class in C++

The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality. Let us check the following example-

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

int main () {

   string str1 = "Hello";
   string str2 = "World";
   string str3;
   int  len ;

   // copy str1 into str3
   str3 = str1;
   cout << "str3 : " << str3 << endl;

   // concatenates str1 and str2
   str3 = str1 + str2;
   cout << "str1 + str2 : " << str3 << endl;

   // total length of str3 after concatenation
   len = str3.size();
   cout << "str3.size() :  " << len << endl;

   return 0;
}

When the above code is compiled and executed, it produces result something as follows −

str3 : Hello
str1 + str2 : HelloWorld
str3.size() :  10

Leave a Reply

Your email address will not be published. Required fields are marked *