C++ Strings

The C++ strings are nothing but used for representing a sequence of characters as an object. String class stores the characters as a sequence of bytes. A string is described by using <string.h> header file. And the character array is simply an array of characters that can be terminated by NULL character. And we can also say that stream defines objects which represent a stream of characters. While the size of the character array has to be allocated statically, and more memory cannot be allocated at the run time if required.

C++ Strings with Examples

In case of strings, memory is allocated dynamically, while the unused allocated memory is wasted in the character array. So more memory can be allocated at run time on demand. There is a threat of character decay in case of the character array. And the implementation of a character array is faster than std:: string. Strings are slower when compared to implementation then character array. While the string class has many constructors that are called to create a string object.

String Declaration in C++

We can represent the strings in two ways. They are as follows:



Syntax: char string_name[string_size];

Through an array of characters

Example:

char greeting[6];

Through pointers

Example:

char *greeting;

C++ supports two different types of string representation. They are as follows:

  • C style character string
  • String  class types introduced with standard c++

C style character string

The string is actually a one-dimensional array of characters which is terminated by a null character ‘\0’. So the null-terminated string contains the characters that comprise the string followed by a null.

Example: char greeting[6] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};

And here we are also providing the memory representation of string

C++ strings

String Initialization in C++

Syntax: char string_name[string_size] = {comma_separated_character_list};

Example:

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

Actually, you don’t need to place the null character (‘\0’) at the end of the string constant. The C++ compiler automatically places at the end of the string.

Example:

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  char greet[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
  cout<<greet<<endl;
  getch();
}

Output:

Hello

Example:

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  char str[40];
  cout<<"Enter your name: ";
  cin>>str;
  cout<<"Hello,"<<str;
  getch();
}

Output:

Enter your name: freshersnow

Hello, freshersnow

Functions that manipulate the NULL-terminated strings

Function Description
strcpy(s1, s2); Copies string s2 into string s1
strcat(s1, s2); Concatenates string s2 onto the end of string s
strlen(s1) Returns the length of string s1
strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1
strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1

Example:

using namespace std;
int main () {
   char str1[10] = "Freshers";
   char str2[10] = "Now";
   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 length of str1 after concatenation
   len = strlen(str1);
   cout<<"strlen(str1) : "<<len<<endl;
   return 0;
}

Output:

strcpy( str3, str1) : Freshers
strcat( str1, str2): FreshersNow
strlen(str1) : 11

String Class in C++

C++ library provides a string class type that supports all the operations, additionally much more functionality.

Example:

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

int main () {

   string str1 = "Freshers";
   string str2 = "Now";
   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;
}

Output:

str1 + str2 : FreshersNow
str3.size() : 11