-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

Monday 22 June 2015

Concept of string

Today I am here with the concept of string in C++.

Remember that the header file for string is <string.h> in C++(may vary in different compilers).

String

A string is used to represent a sequence of characters regarded as a single data item. In C++ strings of characters are held as an array of characters, one character held in each array element. In addition a special null character, represented by `\0', is appended to the end of the string to indicate the end of the string. Hence if a string has characters then it requires an n+1 element array (at least) to store it. Thus the character `a' is stored in a single byte, whereas the single-character string "a" is stored in two consecutive bytes holding the character `a' and the null character.

Remember that the difference between a string data type variable and character data type variable is that a variable with character data type can store single character and it does not include space or the characters after space but a varible with string data type  can store multiple characters and it includes space or the characters after space 

In the below example, the string variable has data type "string".

Source code

#include<iostream>
#include<string.h>
using namespace std;
main()
{
string s1;
s1="Hello welcome to my blog";
cout<<s1;
}

Output

C++,khgamujtaba,strings in C++

Remember that the characters written a string are enclosed in curly braces.

No comments:

Post a Comment