English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ String

In this article, you will learn how to handle strings in C. You will learn to declare them, initialize them, and use them for various inputs/Output operation.

A string is a collection of characters. C ++Programming languages typically use two types of strings:

  • String as a string class object (standard C ++Library string class)

  • C string (C style string)

C style string

In C programming, character sets are stored in the form of arrays, C ++Programming also supports this form. Therefore, it is called C style string (C-string).

c -A string is an array of char types ending with a null character, i.e. \0 (the ASCII value of the null character is 0).

How to define a C string?

char str[] = "C++";

In the above code, str is a string that contains4characters.

Although "c++"has3a character, but the null character \0 is automatically added to the end of the string.

Alternative method to define a string

char str[4] = "C++";
     
char str[] = {'C','+','+','\0'};
char str[4] = {'C','+','+','\0'};

Like an array, you do not need to use all the space allocated for the string. For example:

char str[100] = "C++";

Example1:Using C ++The string reads words

C ++The program displays the string entered by the user.

#include <iostream>
using namespace std;
int main()
{
    char str[100]);
    cout << "Enter string: ";
    cin >> str;
    cout << "You entered: " << str << endl;
    cout << "\nEnter another string: ";
    cin >> str;
    cout << "You entered: "<<str<<endl;
    return 0;
}

Output result

Enter a string: C++
You entered: C++
Enter another string: Programming is fun.
You entered: Programming

Please note that in the second example, only "Programming" is displayed, not "Programming is fun.".

This is because the extraction operator >> is similar to the scanf() method in C language, which considers the space " " as a delimiter.

Example2:C ++The string reads a line of text

C ++The program reads and displays the entire line of user input.

#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 result

Enter a string: Programming is fun.
You entered: Programming is fun.

To read text containing spaces, you can use the cin.get function. This function has two parameters.

The first parameter is the name of the string (the address of the first element of the string), and the second parameter is the maximum capacity of the array.

In the above program, str is the name of the string,100 is the maximum capacity of the array.

String object

In C ++In addition, you can also create a string object to save the string.

Unlike the char array, the string object does not have a fixed length and can be expanded as needed.

Example3:uses the C string data type ++String

#include <iostream>
using namespace std;
int main()
{
    //Declare a string object
    string str;
    cout << "Enter string: ";
    getline(cin, str);
    cout << "You entered: " << str << endl;
    return 0;
}

Output result

Input string: Programming is fun.
You entered: Programming is fun.

In this program, str is declared as a string. Then it asks the user for a string.

In addition to using cin>> or cin.get() functions, getline() can also be used to get input text lines.

The getline() function takes the input stream as the first parameter, cin and str as the position to store the line.

Passing a string to a function

Strings are passed to functions in a similar manner,Arrays can also be passed to functions.

#include <iostream>
using namespace std;
void display(char *);
void display(string);
int main()
{
    string str1;
    char str[100]);
    cout << "Enter string: ";
    getline(cin, str1);
    cout << "Enter another string: ";
    cin.get(str, 100, '\n');
    display(str1);
    display(str);
    return 0;
}
void display(char s[])
{
    cout << "The input character array is: " << s << endl;
}
void display(string s)
{
    cout << "The input string is: " << s << endl;
}

Output result

Enter string: Programming is fun.
Enter another string: Really?
The input string is: Programming is fun.
The input character array is: Really?

In the above program, two strings are required to be input. They are stored in str and str1In which, str is a char array, and str1is a string object.

Then, we have two functions display(), which output strings to strings.

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

This process is called function overloading. Learn more aboutFunction OverloadingMore Information.