C language learning basics (4) string, string array, character and string-related functions
String Introduction
* In Java, a String can be stored as a String.
String s = "MJ ";
The C language does not have the String type. In fact, strings are character sequences consisting of multiple characters. Therefore, in C language, we can use character arrays to store strings.
* A string can be considered as a special character array. to distinguish it from a common character array, an ending sign '\ 0' should be added at the end of the string '.
'\ 0' is a character with an ASCII code value of 0. It is an empty operator, indicating nothing to do. Therefore, the character array is used to store strings. The ending sign '\ 0' should be included in the value assignment '.
* The storage of the string "mj" is as follows (assuming that char a [] is used for storage): char a [] = {'M', 'J ', '\ 0 '};
Note that there is a '\ 0' at the end. If this end mark is not found, the character array is not stored as a string.
String Initialization
Char a [3] = {'M', 'J', '\ 0 '};
Char B [3];
B [0] = 'M ';
B [1] = 'J ';
B [2] = '\ 0 ';
Char c [3] = "mj"; // "during initialization, the system automatically adds a \ 0 terminator to the end of the string.
Char d [] = "mj ";
Char e [20] = "mj ";
When using '''initialization, remember to add '\ 0 ',
String Array Overview
* A one-dimensional character array stores a string, for example, a char name [20] = "mj"
* To store multiple strings, such as the names of all students in a class, a two-dimensional character array is required, char names [15] [20] can store the names of 15 students (assuming the name cannot exceed 20 characters)
* If you want to store the names of two students, you can use a three-dimensional character array char names [2] [15] [20].
String Array Initialization
Char names [2] [10] = {'J', 'A', 'y', '\ 0'}, {'J',' I ', 'M', '\ 0 '}};
Char names2 [2] [10] = {"Jay" },{ "Jim "}};
Char names3 [2] [10] = {"Jay", "Jim "};
Character output function putchar
Putchar (65); //
Putchar ('A'); //
Int a = 65;
Putchar (a); // Aputchar can only output one character at a time
Character Input Function getchar
Char c;
C = getchar ();
* The getchar function can read spaces and tabs until you press Enter. Scanf cannot read spaces or tabs.
* Getchar can only read one character at a time. Scanf can receive multiple characters at the same time.
* Getchar can also read the carriage return line break. At this time, you need to press the return key twice. The carriage return or line break of 1st times is read by getchar. The carriage return key of 2nd times indicates that the input is complete.
The strlen function returns a string with a length not including \ 0, which is calculated until \ 0.
Int size = strlen ("aab ");
Strcpy Function
Char s [10];
Strcpy (s, "lmj"); copy the "lmj" string on the right to the character array s until it is copied to \ 0. A \ 0 string will be retained at the end of s.
Strcat Functions
Char s [20] = "hello"; // Concatenates the string on the right to the end of s. After the connection is complete, A \ 0 string is retained at the end of s.
Strcmp Functions
The two strings are compared from left to right one by one (based on the ASCII value of the characters) until the characters are different or '\ 0' is met. If all characters are the same, the return value is 0.
If they are not the same, the first ASCII value difference of the two strings is returned. That is, if string 1 is greater than string 2, the return value of the function is positive. Otherwise, the return value is negative.
Strcmp (s1, "abd ");
The puts function outputs a string and then wrap it automatically. Only one string can be output at a time to the end of '\ 0'.
Char a [] = {'A', 'B', '\ 0'}; puts (a); puts ("AB ");
Gets Function
The system automatically adds an end mark \ 0 at the end.
Char a [10];
Gets (a); only one string can be read at a time. scanf can read multiple
Gets can read strings that contain spaces and tabs until the carriage return occurs. scanf cannot read spaces or tabs.
When gets and scanf are mixed, it is found that gets is okay before scanf. If scanf is used before gets, gets will not be executed, because gets indicates that 1st of them belong to it.
Scanf Functions
Char a [10]; the system will automatically add an end mark \ 0 at the end
Scanf ("% s", a); because a represents its address, it does not need to be written as &
Scanf ("% f", & a); scanf ("% d, % s, % f", & a, & B, & c); when receiving multiple, the separator can be any symbol.
Including space, tab, and carriage return