String Concatenation
Adding of two or more strings can be said as concatenation.
The + operator can be used between strings to add them together to make a new string .
To write a string , string library is must to add.
ie,#include <string>
#include <iostream>
#include <string>
using namespace std;
int main(){
// string is a datatype and fname, lname and fullName are variables.
string fname = "Mehfila ";
string lname = " Parkkulthil";
string fullName = fname + lname ;
cout << fullName ;
return 0;
}
//output: MehfilaParkkulthil
To create a space between Mehfila and Parkkulthil in output , you need to add a space with double quotes ""
or single quotes ''
.
#include <iostream>
#include <string>
using namespace std;
int main(){
// string is a datatype and fname, lname and fullName are variables.
string fname = "Mehfila ";
string lname = " Parkkulthil";
string fullName = fname + " " + lname ;
cout << fullName ;
return 0;
}
//output: Mehfila Parkkulthil
Top comments (0)