C++ Strings: the difference between string and char data type

C++ difference between string and char data type

Lets us discuss some of the common differences between ‘char’ and string type. We know that a char type variable can hold only single character but a string type can hold multiple characters.

i)
To assign a value to a char variable a single quotation (‘ ‘) is used.But in case of string a double quotation (” “) is used.

char c=’A’ ;
string n=”Hello World” ;

cout<<“char “<<c<<endl;
cout<<“String <<str<<endl;

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string n1="Hello";
	char n2='A';
	cout<<"n1 (string) "<<n1<<endl;
	cout<<"n2 (char) "<<n2<<endl; 
	return(0);
}

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 to a single char is not valid.

string n1;
char n2=’A’;
n1=n2; //Valid

string n1=”Hello”;
char n2;
n2=n1; //Error

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string n1;
	char n2='A';
	n1=n2;
	cout<<"n1 "<<n1<<endl;
	//n1="Hello";
	//n2=n1; //error
	//cout<<"n2 "<<n2<<endl; 
	return(0);
}

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 n1=”Hello”;
string n2=”World”;
string n3=n1+” “+n2;
cout<<“n1 “<<n1<<endl;
cout<<“n2 “<<n2<<endl;
cout<<“n3 “<<n3<<endl;

Output:

n1 Hello
n2 World
n3 Hello World

char n1=’A’;
char n2=’B’;
char n3=n1+n2;
cout<<“n1 “<<n1<<endl;
cout<<“n2 “<<n2<<endl;
cout<<“n3 “<<n3<<endl;

Output:
n1 A
n2 B
n3 â

char n1=’3′;
char n2=’3′;
char n3=n1+n2;
cout<<“n1 “<<n1<<endl;
cout<<“n2 “<<n2<<endl;
cout<<“n3 “<<n3<<endl;

Output:

n1 3
n2 3
n3 f

For char:
n1=’3′ (ASCII Value of 3 is 51)
n2=’3′ (ASCII Value of 3 is 51)
n3=n1+n2; (51+51=102)
cout<<n3;
(output is f (ACSII value 102))

string n1=”3″;
string n2=”3″;
string n3=n1+n2;
cout<<“n1 “<<n1<<endl;
cout<<“n2 “<<n2<<endl;
cout<<“n3 “<<n3<<endl;

Output:

n1 3
n2 3
n3 33

For strings
two strings are added,in such case the second value will be concatenated to the first value so str+str(“3″+”3”) gives “33“.

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string n1="Hello";
	string n2="World";
	string n3=n1+" "+n2;
	cout<<"n1  "<<n1<<endl;
	cout<<"n2  "<<n2<<endl; 
	cout<<"n3  "<<n3<<endl; 
	return(0);
}

Output:

n1 Hello
n2 World
n3 Hello World

#include<iostream>
#include<string>
using namespace std;
int main()
{
     char n1='1';
     char n2='2';
     char n3=n1+n2;
     cout<<"n1 "<<n1<<endl;
     cout<<"n2 "<<n2<<endl;
     cout<<"n3 "<<n3<<endl;
     char n4='3';
     char n5='4';
     char n6=n4+n5;
     cout<<"n4 "<<n4<<endl;
     cout<<"n5 "<<n5<<endl;
     cout<<"n6 "<<n6<<endl;
     return(0);
}

Output:

n1 1
n2 2
n3 c

n4 3
n5 4
n6 g