Strings

A string is a collection of characters. In contrast to C, we can construct a string variable rather than a character array to store a succession of characters. An array stores data of the same type. For example, integers can be kept in an integer array, and a set of characters can be saved in a character array or a string variable. A string is a one-dimensional character array. Declaring a string is similar to declaring a one-dimensional array. It’s just that we’re thinking about it as a cast of characters. The syntax for declaring a string is shown below.

string string_name ;

In the preceding syntax, string name can be any name assigned to the string variable, and it can be given a string input later or even at the moment of definition.

string string_name = "studentproject";

Example of a string:

#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    // declare and initialise string
    string str = "studentproject";
    cout << str << endl;
    return 0;
}

Output:

studentproject
Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in