What is String-How to initialize String in C++

The sequence of characters enclosed in quotation marks and is used to handle non-numeric data i.e., names, addresses etc are called strings.

Defining a String.

General syntax for defining string is given below:
 
data type name_of _string[size of string];
 
In the above general syntax, data type, is char data type and name_of_string is the name of the string. 
 
The size(number of characters) of the string is specified in square brackets, []. The following statement defines a string with the name str having size 20.
 
char str [20]
 
For handling strings, the header file <cstring> is needed but in some implementations this library may be automatically included when the header file <iostream> is included.
 
There are some limitation of string in C. To overcome and remove these problems, C++ provides a class <string> that removes may of these problems.
 
The general syntax for defining strings in C++ is given below:
 
sting str;
 
This statement defines a string name str of type string.


Read: What is One Dimensional Array- How to initialize One Dimensional Array in C++

Initializing String.

String can be initialized as arrays are initialized because string is character array.
 
Consider the following example:
char greeting [6] = {‘H’,’e’,’l’,’l’,’o’,”};
 
The above statement declares a string named “greeting ” with six elements of type char that are initialized with the characters that form the word “Hello” plus a null character ” at the end. Another method for initializing a string is to enclosed the string in double quotes, “” as shown below:
 
char greeting [] = “Hello”;
 
In both cases, the array of characters greeting is declared with a size of 6 elements of type char the 5 characters that compose the word “Hello”.
 
Consider an example to ask for your name and then display a greeting message on the screen as shown below using string:
 
#include<iostream.h>
#include<conio.h>
int main()
{
char question[] = “Please enter your first name”;
char greeting[]= “Hello,”;
char yourname[80];
cout<<question;
cin>>yourname;
cout<<greeting<<yourname<<“!”;
getch();
return 0;
}
 
Output of the program
 
Please enter your first name:
Yaseen
Hello, Yaseen
 
String in C++ can be initialized both way like in C and directly like ordinary variables with the only difference that the string should be enclosed in double quotes.

Consider the following initialization example:
 
sting str1(Call me);
string str2 = “Send sms!”;
string str3(“Okay”);
 
Here, in these examples three string: str1, str2 and str3 and directly initialized to “Call me”. “Send sms”,”Okay” respectively. Note that while using C++ string in programs, the class string must be included.
 
Consider the following program to demonstrate initialization of C++ string:
 
//C++ strings initialization program
#include<iostream.h>
#include<conio.h>
int main()
{
sting str1(Call me);
string str2 = “Send sms!”;
string str3(“Okay”);
cout<<“First string str1 is:”<<str1<<endl;
cout<<“Sirst string str2 is:”<<str2<<endl;
cout<<“Third string str3 is:”<<str3<<endl;
getch();
return 0;
}
 
Output of the program
 
First string str1 is: Call me
Second string str2: Send sms
Third string str3 is: Okay
The following program demonstrates some more features of C++ string e.g., concatenation, copying and finding size of string.
 
#include<iostream.h>
#include<conio.h>
int main()
{
string str1= “Hello”;
string str2 = “World”;
string str3, str4l
int len;
str3= str1; //string copying
str4=str1+str2; //string concatenation
len=str4.size(); //finds string size.
cout<<“str3=t”<<str3<<endl;
cout<<“str4=t”<<str4<<endl;
cout<<“length of str4=t”<<len<<endl;
getch();
}
 
Output of the program
 
str3=     Hello
str4       HelloWorld
length of str4 = 10