String type is a built-in type in C++ which can hold many characters,so it can represent a sentence or a lines of text.A char type can represent/hold only a single letter at a time but string type can hold many characters -this is one of the difference between string and char data type,more differences is discuss later.So,we can say a string is a collection of char type.A String type has a size of 4 bytes and it’s keyword is string.A simple usage of string type is given below.
using namespace std ;
int main( )
string str=”Kaidou is coming!” ;
cout<< str << endl ;
The output is,
Kaidou is coming!
If you are familiar with C then note that the concept of string in C and C++ are different.One of the difference is:security,C++ string type is more secure than C string type,while using string C++ string type is recommended.We will have more detail discussion on ‘string’ in Chapter 7.
Uses of string type
i) A string variable can hold many characters including numbers,special letters,escape sequence,etc. So if you want to make a string that can represent both character and numbers combine ,say for instance you want to represent a date with slashes between a date ,month and year then string is the perfect choice.Consider the code below.
cout << date ;
ii) Another uses of string is to represent a real number value with very large precision and accuracy.We know floating point type can represent a real number,but it can represent maximum only up to six decimal point value.If you command the compiler to output more than six decimal point using a standard function,it will but it won’t be accurate.
double d=123.48763567328832973939823623342 ;
cout<< d << endl
<< setprecision(25) << endl; //forcing compiler to output up to 25 precision value
The output is,
After the 14th decimal point digit the value loses it’s accuracy.This can create a grievous situation in Mathematics calculation requiring high accurate value.So a better solution is to use string to represent such value.
cout<< value ;
Now the value will not lost it’s accuracy even up to say 200 precision value.A detail discussion on precision and accuracy in C++ is given here.
Difference between string and char data type
i)To assign a value to a char variable a single quotation ” is used.But in case of string a double quotation “” is used.
string str=”Happy string!” ;
cout<< c << ” ” << str ;
There is no specific reason as to why a char literal is represented under single quotation and string literal under double quotation.The best reason can be they are made to do so.Another reason is language heredity.Since C was using this syntax and C++ was made from C so C++ too went with this syntax.
ii)A char type value can be assigned to string variable but the vice versa is not true.This behavior is reasonable since char consist of only one value converting to string type which can hold many character value is not problematic.But if we look at the vice versa process converting many characters string to a single char is just outrageous,I mean which is the lucky character among many characters in string that must be chosen to assign to the char variable;it wouldn’t make any sense.So it is not allowed.
string str , str1=”Name:” ;
/*c1=str1 ; Error
cout<< str << ” ” << c ;
iii)We can add two strings (with + sign ) and assign the resultant to string and also we can add two characters and assign the resultant to a char variable but,they will yield a different output.
string str , str1 ;
cout<< str << endl
<< str1 << endl
<< c << endl
<< c1 ;
The output is,
For str and c1 two characters are added so in performing c+c their corresponding integer value are added which means c+c is same as 51+51(=102) and the resultant value is assigned to the variable str and c1.And since 102 int value correspond to the character ‘f’ we got the output ‘f’. But for str1 two strings are added,in such case the second value will be concatenated to the first value so str+str(“3″+”3”) gives “33“.