String Class in C++
The standard C++ library provides a string class type that supports all the operations related to string class.
Include the string library
#include <string>
To Create a string variable
1.
string n = “Hello”;
2.
string n1;
n1=”Hello World”
#include<iostream> #include<string> using namespace std; int main() { //1 string n; n="Hello"; cout<<"string is "<<n<<endl; //2 string n1="Hello World"; cout<<"string is "<<n1<<endl; return(0); }
Note: cannot take input of string variable using gets()
cout<<“Enter any string “;
gets(n); //error
cout<<“String is “<<n<<endl;
#include<iostream> #include<string> #include<stdio.h> using namespace std; int main() { string n; //Note: cannot take input of string variable using gets() cout<<"Enter any string "; gets(n); //error cout<<"String is "<<n<<endl; return(0); }