Using character arrays to store and process strings

string

A string is a sequence of characters enclosed in double quotes. For example, "China". String constants add '\0' as the end mark at the end of the character sequence. The string is stored in memory in the order in which the characters in the string are arranged, and '\0' is added at the end as the end mark.

For ASCII code, each character occupies one byte, while for Unicode code, each character occupies two bytes. This tutorial only talks about ASCII encoded characters and strings.

C++ is the same as C. There are no string variables in the variables of basic data types. So how to deal with the storage and operation of strings?

In C language, character arrays are used to store strings, and the operations are similar to ordinary arrays. The C++ language inherits this approach. In addition, the string class is also defined in the standard C++ library.

Declaration and use of character arrays

The declaration and use of character arrays are the same as those of other types of arrays. The syntax for declaring a character array is:

char 字符数组名[下标表达式1][下标表达式2]...;

Let me give you a simple example about the declaration and use of character arrays:

#include <iostream>
using namespace std;
int main()
{
    
    
    char str[10] = {
    
    'I', ' ','l','o','v','e',' ','y','o','u' };        // 声明和初始化一维字符数组str
    int i;
    for (i=0; i<10; i++)
    {
    
    
        cout << str[i];
    }
    cout << endl;
    return 0;
}

Insert image description here

Strings are stored and processed using character arrays

In the above example, a character array is defined and the elements in the array are processed one by one, but what is stored is not a string because there is no end mark '\0' at the end. If we add '\0' at the end when initializing the assignment to the character array, it forms a string, but the number of elements in the array used to store the string should be greater than or equal to the length of the string plus 1.

When initializing a character array, the initial value can be a comma-separated character constant or ASCII code, or a string constant. For string constants, there is no need to explicitly add '\0' at the end, '\0' is implicitly included.

Based on the above methods of initializing and assigning values ​​to character arrays, here are a few examples:

char str[5] = {
    
    108,111,118,101,0};
// 以逗号分隔的ASCII码为字符数组初始化赋值
char str[5] = {
    
    'l','o','v','e','\0'}; 
// 以逗号分隔的字符常量为字符数组初始化赋值
char str[5] = "love"; 
// 以字符串常量为字符数组初始化赋值

When a string is stored in a character array, we can process and output it character by character as in the above routine, or we can input or output the entire string at once.

For example:
one-time input: char str[5]; cin >> str;
one-time output:char str[5]="love"; cout << str;

When inputting or outputting a string at one time, we should pay attention to:
1. For the string end mark '\0', the output string will not be output.
2. When entering multiple strings, they need to be separated by spaces. If you want to enter a single string, there must be no spaces, otherwise it will be considered as multiple strings.
3. When outputting a string, the output parameter is the character array name, and the output ends when '\0' is encountered.

Give examples to illustrate the above three points:
1. char str[5]="love"; cout << str;. The end of the string "love" implies '\0'. When outputting, only "love" will be output and no '\0' will be output.
2. char str1[5],str2[5],str3[5]; cin >> str1 >> str2 >> str3;When the program is executed and "I love you" is entered, the strings str1, str2 and str3 will be assigned the values ​​"I", "love" and "you" respectively. If instead char str[11]; cin >> str;"I love you" is entered when the program is executed, str is assigned the value "I". Because a space is entered after 'I', it is considered to be multiple strings, and str is assigned only the substring before the space.
3. When outputting a string, the cout parameter only needs to write the name of the character array. For example, cout << str can output the str string, and the output ends when '\0' is encountered.

We can use the string processing functions in the library to process strings. For example, strcat is used to connect two strings, strcpy is used to copy strings, strcmp is used to compare strings, and strlen is used to calculate the length of strings. . You need to include the header file string.h before using these functions.

Guess you like

Origin blog.csdn.net/qq_36314864/article/details/117882266