Introduction to String in C++
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 which are surrounded by the double quotations. Any continuous characters assigned to a variable is called String variable. Here, let us see the usage of String data type in C++ programming language.
Syntax:
Below is the syntax for the string data type:
string (data type ) trying (variable) = "Test" (Value assigned to variable)
Explanation: The texts that are written inside the brackets in the above syntax is regarding how the syntax should actually be understood. According to syntax which is already above, a variable that is represented with the ‘string’ data type becomes the string variable and the string variable is assigned to a value. This part of assigning a variable to its value is called initialization.
Syntax:
string (data type) trying_2 (variable) ("Test in another way") à (value assigned to variable);
In the above representation, the data type and the variable is assigned to the value without the “equal to” assignment operator again using the brackets and quotes.
Rules and Regulations
The main observation that has to be done with respect to the C++ string is that “String” is also a library which deals with any C++ functions.
In C++ two types of string representation format are feasible. One way is by using the “char” data type as used in C programming language and the other is by using the string data type itself. The “char” data type is used with the representation of the array. But it is recommended to use the ‘string’ data type as the ‘char’ array which would be defined is static in nature. If the content value is less than the size of the array represented, then the extra space gets wasted. On the other hand, ‘string’ is dynamic in nature.
One must be careful in assigning and initializing values to “String”. If we are looking at initializing value to string by means of an array that would definitely give us the error. So we need to use the “char” data type for the same. Below is the example for the same:
Code:
#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;
Output:
- As per the output, the array declaration would give the output of the location of value which is stored.
Code:
#include <iostream>
using namespace std;
int main()
{
string big="I am writing many words";
cout<<"The output here is: "<<big;
Output:
- Now, let us see how we can change the character in the string given.
Code:
#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;
}
Output:
- Let’s now give the string as the user input value, which is obviously simple and easy.
Code:
#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;
Output:
- The output you got here is only until the compiler encounters a space.
- Now to get the whole line that the user had given as input, the following can be done:
Code:
#include <iostream>
using namespace std;
int main()
{
string r;
cout<<"Enter any string of your choice"<<endl;
getline(cin,r);
cout<<r;
}
Output:
- Using the “getline” function we can have the complete user given input under the output.
- The ‘cstring’ library helps us in such a way that we can use different functions that are built-in that library. Some of them are strcat, strcmp, strcpy, strlen, etc, which deals with string concatenating, comparing, copying and finding the length of the string, respectively.
Examples of String in C++
Let us see below the example related to string:
Example #1
Code:
#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;
Output:
Example #2
Now let us take a condition without having the declaration of the namespace.
Code:
#include <iostream>
//using namespace std;
int main()
{
string trying_1="test";
cout<<"Printing the string data type value: "<<trying_1<<endl;
Output:
Example #3
Now, what if we use the std function before and check the output:
Code:
#include <iostream>
//using namespace std;
int main()
{
std::string trying_1="test";
std::cout<<"Printing the string data type value: "<<trying_1<<std::endl;
Output:
Example #4
Let us have a small program detailing for a string library with char data type:
Code:
#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;
Output:
Now, try the same by keeping the data type as string instead of char and analyze the output. So here, this is not only for the “String” functions or data type but using the namespace declaration is important, else we have to use “std” in front of each declaration to make it productive.
Conclusion
So, here we have learnt about different modules with respect to Strings in C++ programming language. Strings are a very important concept in any programing language as it deals with analyzing and implementing continuous characters. Learning how to use them is necessary in writing any different and complete programs. Keep practicing and enjoy learning C++.
Recommended Articles
This is a guide to String in C++. Here we discuss the introduction, syntax and various examples of string in c++ with proper code and outputs. You may also look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses