String in Cpp

String in Cpp


What is String?

A string is a sequence of characters.

How to declare a String?

Syntax :

string variable_name;

Example :

string eruditors;

//here string indicates the data type
//eruditors is the name of the string


How to initialize a String?

A string can be initialized in different ways. There are two main ways to initialize a string in C++.

1. C-String

char str[10]={'e', 'r', 'u', 'd', 'i', 't', 'o', 'r', 's', '\0'};

//we can initialize a string without specifying the string size also. For example,

char str[]={'e', 'r', 'u', 'd', 'i', 't', 'o', 'r', 's', '\0'};

//here in both cases we are initializing strings named ‘str’ which contains the value ‘eruditors’

char str[10]="eruditors";

//now, we’ll declare without declaring the string size

char str[]="eruditors";

//here in both cases we are initializing strings named ‘str’ which contains the value ‘eruditors’

2. By String Class

string eruditors = "Welcome to Eruditors";


How to read a string from a user ?


Syntax :

string string_name;
getline(cin, string_name);

//here at first we are creating a string named string_name
//then we are taking a string as user input using 'getline()' function, as it takes the entire line as a text

char string_name[25];
cin.get(string_name, 25);

//we can also take user input using cin.get() function in case of C-String


String Input/Output Program in C++ :


#include <iostream>
using namespace std;

int main()
{
    string eru1;
    char eru2[25];

    cout << "Enter a string : ";
    getline(cin, eru1);

    cout << "Enter another string : ";
    cin.get(eru2, 25, '\n');
    
    cout << "Entered char array is : " << eru2 << endl;
    cout << "Entered string is : " << eru1 << endl;

    return 0;
}


Output :



String operations using C++ Library Functions :

A programmer needs to do many kinds of works with Strings like copy, swap and many more. It becomes very complicated if we write lines of codes to perform these tasks. C++ standard libraries contains many functions that do those works very easily. "string" header file's "string" class contains many useful functions that makes coding very easy. For example,

copy(char_array, len, pos) - It is used to copy the substring in target character array mentioned in its arguments. It takes 3 arguments, target char array, length to be copied and starting position in string to start copying.
push_back() - This function is used to input a character at the end of the string.
pop_back() - This function is used to delete the last character from the string.
swap() - This function swaps one string with other. str1.swap(str2) in this way the strings can be swapped.
capacity() - This function returns the capacity allocated to the string, which can be equal to or more than the size of the string.
resize() - This function changes the size of string, the size can be increased or decreased.
length() - This function finds the length of the string
shrink_to_fit() - This function decreases the capacity of the string and makes it equal to the minimum capacity of the string.