C++

C++ Strings

C++ Strings

Strings are used for storing text characters. A string variable contains one or more characters inside double quotes. C++ uses C style character strings as well as has its own string class type. 

The C-style character string has been taken from the C language. This string is nothing but a 1-D array of characters terminated by a null character '\0'. For e.g.

char vowels[6] = {'a', 'e', 'i', 'o', 'u', ’\0’};

The last character in vowels will be '\0' that gets placed by compiler automatically when it will initialize vowels array. So you string would have one space extra in the end. Let us see what happens when we try to make vowels array with 5 characters instead of 6. Save the following cope in a file say checkString.cpp

#include <iostream>
 
using namespace std;

int main () {

   char vowels[5] = {'a', 'e', 'i', 'o', 'u'};

   cout << "Vowel list: ";
   cout << vowels << endl;

   return 0;
}

Now compile and run it.
(base) 15:30:13:~ % g++ checkString.cpp 
(base) 15:30:19:~ % ./a.out 
Vowel list: aeiou

C++ supports a wide range of functions that manipulate null-terminated strings. For e.g. strcpy (copy one string to another) , strcat (one string to the end of other), strelen (find length of the string) strcmp (compare two string for equality) etc.  The following code shows application of some of these functions. Copy the following code into  a file say CstringFunctions.cpp

// Header for cin/cout streamhandling
#include <iostream>
// Header for C type null terminated string functions
#include <cstring>

using namespace std;

int main ()
{

   char strA[10] = "Go";
   char strB[10] = "Corona";
   char strC[10];
   char strFinal[20];
   int  len ;

   // copy strA into strC
   strcpy( strC, strA);
   cout << "strcpy( strC, strA) gives strC as " << strC << endl;

   //Compare two strings
   if (strcmp(strA, strC) == 0)
       cout<<"strA and strC are same"<<endl;
   else
       cout<<"strA and strC are not same"<<endl;

   // concatenates strFinal with strA and strB and strC with space in between
   strcat(strFinal, strA);
   strcat(strFinal, " ");
   strcat(strFinal, strB);
   strcat(strFinal, " ");
   strcat(strFinal, strC);

   cout << "strcat operartion on strFinal, strA, strB, str C gives strFinal as " << strFinal << endl;

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

   return 0;
}
(base) 15:51:26:~ % g++ cStringFunctions.cpp
(base) 15:51:30:~ % ./a.out                 
strcpy( strC, strA) gives strC as Go
strA and strC are same
strcat operartion on strFinal, strA, strB, str C gives strFinal as Go Corona Go
strlen(strFinal) : 12

Let us move to C++ string class that is new addition. To use strings, you must include an additional header file in the source code, the <string> library. string class provides standard container of bytes, with additional features specifically designed to operate with strings of single-byte characters. The string class is an instantiation of the basic_string class template that uses char (i.e., bytes) as its character type. It uses its default char_traits and allocator types. We can perform many operations on strings such as concatenation, comparison, conversion etc. Let us implement the same program using C++ string. Class and its method. Copy the following code into  a file say stringFunctions.cpp

// Header for cin/cout streamhandling
#include <iostream>
// Header for C type null terminated string functions
#include <string>

using namespace std;

int main ()
{

   string strA = "Go";
   string strB = "Corona";
   string strC;
   string strFinal;
   int  len;

   // copy strA into strC
   strC = strA;
   cout << "After copying from strA, strC is " << strC << endl;

   //Compare two strings
   if (strA == strC)
       cout<<"strA and strC are same"<<endl;
   else
       cout<<"strA and strC are not same"<<endl;

   // concatenates strFinal with strA and strB and strC with space in between
   strFinal = strA + " " + strB + " " + strC;

   cout << "After appending strA, strB and str C, strFinal becomes " << strFinal << endl;

   // total length of str1 after concatenation
   len = strFinal.size();
   cout << "strFinal.size() gives len of strFinal as " << len << endl;

   return 0;
}

Now compile and run the code. You will get the same output

(base) 16:01:53:~ % g++ stringFunctions.cpp
(base) 16:01:54:~ % ./a.out 
After copying from strA, strC is Go
strA and strC are same
After appending strA, strB and str C, strFinal becomes Go Corona Go
strFinal.size() gives len of strFinal as 12