String is a datatype which is mostly used in programming or in your codes. String is basically a series of characters. The “CHAR” datatype only contains one character per variable. Strings are easy to manipulate. In c++ the string datatype cannot be used by default, to use string and its functions in your codes we have to include a header file “string” in our code or program.
Declaration & Initialization:
To initialize a string variable following syntax is used.
in this line str is name of variable.
Getting Size of String:
we can get the size of string by using the function size() defined in string library.
Converting String to Char:
While processing strings we often needed to convert it into char array. We can do it as follows.
in this .c_str function is used to convert it into stream of character.
Following is the fully compiled code.
#include<iostream> // for basic input output operations
#include<string> // for using string operation and datatype
using namespace std; //using standard namespaces
cout<<”Size Of String is=”<<str.size()<<endl; //printing size of string
cout<<str<<endl; //printing the string
char *a=(char*)str.c_str(); //converting string to charater
cout<<a<<endl; //display full charaters
// printing each element of character array
while(a[i]!=’\0′) //’\0′ is a null character which is placed at the end of charater array
return 0; //successfully terminated program