Software Developer
The string is a library function in C++, which helps in performing all the string-related operations in the program. And there is a ‘string’ data type that is assigned to a variable containing a set of characters that are surrounded by double quotations. Any continuous characters assigned to a variable is called String variable. Here, let us see the usage of String data type in the C++ programming language.
Below is the syntax for the string data type:
string (data type ) trying (variable) = “Test” (Value assigned to variable)
string (data type) trying_2 (variable) ("Test in another way") à (value assigned to variable);
In the above representation, the data type and the variable are assigned to the value without the “equal to” assignment operator again using the brackets and quotes.
The main observation that has to be done concerning the C++ string is that “String” is also a library that deals with any C++ functions.
In c++ two types of string representation codecs are possible. One manner is through the usage of the “char” facts type as used in the interval and the alternative is through the usage of the string records type itself. The “char” statistics type is used with the illustration of the array. However, it's far recommended to use the ‘string’ facts kind as the ‘char’ array which would be described as static. If the content material value is much less than the scale of the array represented, then the extra space receives wasted. However, ‘string’ is dynamic.
One must be careful in assigning and initializing values to “String”. If we are looking at initializing value to a string using an array that would give us the error. So we need to use the “char” data type for the same. Below is the example for the same:
#include <iostream>
using namespace std;
int main()
{
string ex1="example1";
string ex2[]="example2";
char ex3[]="example3";
cout<<"The first exxample: "<<ex1<<endl;
cout<<"The second example: "<<ex2<<endl;
cout<<"The third example: "<<ex3<<endl;
#include <iostream>
using namespace std;
int main()
{
string big="I am writing many words";
cout<<"The output here is: "<<big;
#include <iostream>
using namespace std;
int main()
{
string h="Happy";
cout<<"The output here is: "<<h<<endl;
h[1]='A';
cout<<"The output here is: "<<h;
}
#include <iostream>
using namespace std;
int main()
{
string r;
cout<<"Enter any string of your choice"<<endl;
cin>>r;
cout<<"The output here is: "<<r;
#include <iostream>
using namespace std;
int main()
{
string r;
cout<<"Enter any string of your choice"<<endl;
getline(cin,r);
cout<<r;
}
Let us see below the example related to the string:
#include <iostream>
using namespace std;
int main()
{
string trying_1="test";
string trying_2 ("Test in another way");
cout<<"Printing the string data type value: "<<trying_1<<endl;
cout<<"Another print data: "<<trying_2;
Now let us take a condition without having the declaration of the namespace.
#include <iostream>
//using namespace std;
int main()
{
string trying_1="test";
cout<<"Printing the string data type value: "<<trying_1<<endl;
Now, what if we use the std function before and check the output:
#include <iostream>
//using namespace std;
int main()
{
std::string trying_1="test";
std::cout<<"Printing the string data type value: "<<trying_1<<std::endl;
Let us have a small program detailing for a string library with char data type:
#include <iostream>
using namespace std;
#include <cstring>
int main()
{
char r[10]="hello";
char e[5]=" hi";
cout<<"String r is equal to: "<<r<<endl;
cout<<"String e is equal to: "<<e<<endl;
strcat(r,e);
cout<<"The output here is: "<<r;