C++ Strings

By | March 15, 2020

A String is a one-dimensional array of character data types which ends with a null value represented by ‘\0’. The array of character always terminates with a null value, so if there is a character array(string) of size 6 and it has only 5 elements filled, so the 6th place would be a null value.

To initialize a string

There are two techniques by which we can initialize a string:

Vamware
char string[10]={'t','e','c','h','g','e','e','k'};

OR

char string[10]="techgeek";

string[10] value stored as:

------------------------
|t|e|c|h|g|e|e|k|'\0'| |
------------------------

Note: The string should always reside in double quotes “ ”.

‘a’ Vs “a” (Character Vs String)

If any value is inside single quotes then it considered as a Character in C++ and if its inside double quote it considerer String:

So here ‘a’ represent a single character and,

“a” represents string.

Example

#include <iostream>
using namespace std;  

int main()
{             
     char ch = 'T', string[20]= "TechGeekBuzz";
     cout<<"The value ch store is: "<<ch;
     cout<<endl;

     cout<<"The value string store is: "<<string;
     return 0;              
}

Output

The value ch store is: T

The value string store is: TechGeekBuzz

Behind the code

Here ch and string belong to the same data type char, but both represent different data structure. ch is a character data type whereas string is a character array data structure. That’s why ch is only able to store one character and string is able to store 20 characters.

C++ in-built String Function

C++ has a string library that contains some C++ string in-built functions. To use these string functions we need to mention the header file name of the string function, which is <#include string.h>.

String Function Description
strcpy(str1, str2);

 

Copy the value of string str2 into string str1
strcat(str1, str2);

 

Concatenate string str2 with string str1. Join the value of str2 in str1
strlen(str1);

 

It returns the total number of characters present in the string.
strcmp(str1, str2);

 

It compares str2 with str1, and return 0 if str1 and str2 are same, returns less than 0 if str1 is smaller than str2 and return greater than 0 if str1 is greater than str2.
strchr(str1, ch);

 

It returns the pointer to the first occurrence of character ch in the string str1.
strstr(str1, str2);

 

Returns a pointer to the first occurrence of string str2 in string str1.

Example

#include <iostream>
#include<string.h>
using namespace std;
int main()
{             
      char str1[10]="tech", str2[10]="geek", str3[10], ch='t';      
      strcpy(str3,str1); // copy the value of str1 into str3
      cout<<"The value of str3: "<<str3;
      cout<<endl;
     cout<<"The value of strcat(str1, str2) is: "<<strcat(str1,str2); //add the value of str2 in str1
     cout<<endl;
     cout<<"The length of str2 is: "<<strlen(str2); // print the length of str2
     cout<<endl;      
}

Output

The value of str3: tech
The value of strcat(str1, str2) is: techgeek
The length of str2 is: 4

Leave a Reply

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