Home > C++ > Strings in C++

Strings can be considered as characters in sequential order. They contain characters in a sequential order. A c++ string can contain any type of characters in it.
E.g.

“Hello”  
“This is a string with spaces.”

Both above-written lines are strings.
In c++ strings can be stored in two ways:


1.C++ String in a character array

We can store strings in character arrays as each character of string at one index in an array.
E.g.

char str[5] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’};

If we use above written array with cout statement i.e. to show user we will have hell at our console screen or we will see some garbage value appended with string stored. Why is that?

#include "stdafx.h"
#include"iostream"

using namespace std;

int main()
{
     char str[5] =  { 'h', 'e', 'l', 'l', 'o' };
     cout << str << endl;
}

Output: –
string

The answer to this is that each string ends with a null character i.e. ‘\0’ which is automatically inserted at the end of the string by the compiler when a string is created.

Note: – Size of character array storing string must be one greater than the size of the string. i.e.

If we want to save a string of 6 characters in an array, the size of our array must be kept at-least 7.
E.g.

#include "stdafx.h"
#include"iostream"

using namespace std;

int main()
{
     char str[7] =  { 'S', 't', 'r', 'i', 'n','g' };
     cout << str << endl;
}

Output: –
string example

Storing strings with spaces in Character Arrays: –
We can store strings with space in character array. It is done the same way as above. Considering each space as a character and storing it.
E.g.

#include "stdafx.h"
#include"iostream"

using namespace std;

int main()
{
     char str[12] =  { 'h', 'e', 'l', 'l', 'o', ' ','w', 'o', 'r', 'l', 'd' };
     cout << str << endl;
}

Output: –
string with spaces

We can take input from a user and store it as a string in character array as follow:

#include "stdafx.h"
#include"iostream"

using namespace std;

int main()
{
     char str[11];
     cout  << "***String Input***" << endl;
     for (int i = 0;i < 10;i++)
     {
          cout << "Please Enter Character : ";
          cin >> str[i];
     }
     cout << "***String Output***" << endl;
     for (int i = 0;i < 10;i++)
     {
          cout << str[i];
     }
     cout << endl;
}

Output: –
creating a C++ string in a character array

Note: – We cannot add space in the string when we take input from the user.
To resolve this problem there is another way to store strings in c++.


2. C++ String by Using Standard String Library

It is necessary to include string header file in the program to store strings in this way.
Syntax to store strings using string library is:

string variable-Name = "string to be stored written inside inverted commas";

E.g.

string str = "Hello world";

#include "stdafx.h"
#include"iostream"
#include"String"

using namespace std;

int main()
{
     string str = "Hello world";
     cout << str << endl;
}

Output: –
strings

Taking input from the user: –

#include "stdafx.h"
#include"iostream"
#include"String"

using namespace std;

int main()
{
     string str;
     cout << "***String Input***" << endl;
     cout << "Please Enter String : ";
     cin  >>  str;
     cout << "***String Output***" << endl;
     cout << str << endl;
}

Output: –
strings user input

One thing to be noted here is that we input “Hello World” as a string but in output, it only shows “hello”. Why is that?
This is because cin will input strings without spaces only. If we want to input a string with spaces we need to use a function named as:

getline(cin, string-variable-Name);

E.g.

#include "stdafx.h"
#include"iostream"
#include"String"

using namespace std;

int main()
{
     string str;
     cout << "***String Input***" << endl;
     cout << "Please Enter String : ";
     getline(cin, str);
     cout << "***String Output***" << endl;
     cout << str << endl;
}

Output: –
c++ string with spaces

There are a number of built-in C++ functions available for strings.
strcpy(); This function copies 1st string contents to 2nd string.
strlen(); It returns the total length of the string passed as an argument.
size(); It also returns the length of the string.
strcat(); This function Concatenates two strings passed as arguments i.e. 2nd string at the end of the first string.

Leave a Reply

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