c++

C++ String

What is string?

In C++ , there is a header file called <string.h> which contains a huge collection of predefined functions performing various operations on strings. So, as a programmer if we use this header file then we can very much simplify our programming efforts as for most of the operations we will not have to write our own logics and we can directly perform those operations using the header file <string.h>.

There are two types of strings commonly used in C++ :-


What are C-strings ?

In C programming, the collection of characters is stored in the form of arrays, this is also supported in C++ programming. These stored arrays are called C-strings.

C-strings are arrays of type char terminated with null character, that is, \0 (ASCII value of null character is 0).

How to define a C-string in an array?

char str[] = "C++";

In the above code, str is a string and it holds 4 characters.

But in, "C++" has 3 character, the null character \0 is added to the end of the string automatically.

Alternative ways of defining a string :-

char str[4] = "RAM";
char str[] = {'R','A','M','\0'};
char str[4] = {'R','A','M','\0'};

Like arrays, it is not necessary to use all the space allocated for the string.

For example:

char str[10] = "RAM";

Example 1 : C++ String to read a word

C++ program to display a string entered by user.

#include <iostream>

using namespace std;

int main() {
    char str[100];

    cout << "Enter a string: ";
    cin >> RAM;

    cout << "You entered: " << RAM << endl;

    cout << "\n Enter another string: ";

    cin >> RAM;

    cout << "You entered: " << RAM << endl;

    return 0;
}

Output :

Enter a string: RAM
You entered: RAM

Enter another string: Running keeps us fit.
You entered: Running

Notice that, in the second example only "Running" is displayed instead of "Running keeps us fit".

This is because the extraction operator >> works as scanf() in C and considers a space ' ' has a terminating character.

Example 2 : C++ String to read a line of text

C++ program to read and display an entire line entered by user.

#include <iostream>

using namespace std;

int main() {
    char str[100];

    cout << "Enter a string: ";
    cin.get(str, 100);

    cout << "You entered: " << str << endl;

    return 0;
}

Output :

Enter a string: Running keeps us fit.
You entered: Running keeps us fit.

To read the text containing blank space, “cin.get” function can be used. This function takes two arguments at a time.

First argument is the name of the string (address of first element of string) and second argument is the maximum size of the array.


String Object

In C language , you cannot create a string object for holding strings but C++ , allows us to create string object for holding strings.

Unlike using character arrays, string objects has no fixed length, and can be extended as per our requirement.

Example 3: Print using string data type

#include <iostream>
                
using namespace std;

int main(){
    // Declaring a string object
    string str;

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

    cout << "You entered: " << str << endl;

    return 0;
}

Output :

Enter a string: Running keeps us fit.
You entered: Running keeps us fit.

In this program, a string “Running is fun” is declared. Then the string is asked from the user.

Instead of using cin >> or cin.get() function, you can get the entered line of text using getline().

“getline()” function takes the input stream as the first parameter which is “cin” and “str” as the location of the line to be stored.


Passing String to a Function

Strings are passed to a function in a similar way as “ arrays are passed to a function”.

#include <iostream>

using namespace std;

void display(char *);

void display(string);

int main(){
    string str1;
    char str[100];

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

    cout << "Enter another string: ";
    cin.get(str, 100, '\n');

    display(str1);

    display(str);

    return 0;
}
void display(char s[]){
    cout << "Entered char array is: " << s << endl;
}
void display(string s){
    cout << "Entered string is: " << s << endl;
}

Output :

Enter a string: Running keeps us fit.
Enter another string: Really?
Entered string is: Running keeps us fit.
Entered char array is: Really?

In the above program, two strings are asked to enter. These are stored in str and str1 respectively, where str is a “char” array and str1 is a string object.

Then, we have two functions display() that outputs the string onto the string.

The only difference between the two functions is the parameter. The first display() function takes char array as a parameter, while the second takes string as a parameter.

This process is known as function overloading.