1. Character Arrays1. Character Type(1) Character Constant: const char constant = ‘character’;(2) Character Variable Format: char variable_name;(3) ASCII Values: A-65, a-97, 0-48. The difference between a and A is 32.2. Character Arrays(1) Format: char array_name[element_count];(2) The value of a single character variable is enclosed in single quotes. Strings are enclosed in double quotes.(3) The null character “\0”, \n represents a newline, \r represents a carriage return, and \t represents a tab, moving to the next tab position.(4) The difference between character arrays and strings is whether there is a string termination character ‘\0’.2. Strings1. Format: string variable_name;2. The header file for strings is #include <string>3. Strings can contain Chinese characters.4. A character constant occupies 1 byte, while a string constant occupies bytes equal to the string byte count plus 1. The additional byte stores the string termination character \0.5. Space characters, carriage return characters, and newline characters can all be considered as string termination symbols. When using cin to read a string, there cannot be spaces in between.6. To read a complete string that contains space characters, carriage return characters, and newline characters, use the getline function. Format: getline(cin, string_variable_name);7. Use scanf() and printf() for string input/output. scanf(“%s”, string_name);, printf(“%s”, string_name);.8. Use gets() and puts() for string input/output. gets(string_name);, puts(string_name);. Using gets and puts requires including the header file cstring.3. String Functions1. strlen(s) returns the length of string s, returning an integer.2. strcpy(s1, s2) copies s2 to s1, overwriting the s1 string.3. strcat(s1, s2) appends s2 to the end of s1.4. strcmp(s1, s2) compares the sizes of s1 and s2. If s1 > s2, it returns greater than 0; if s1 < s2, it returns less than 0; if s1 == s2, it returns equal to 0.5. string_name.size(s1) or string_name.length(s1) outputs the length of string s1.6. string_name.insert(i, s) inserts string s at position i in the string.7. string_name.erase(i, len) deletes len characters starting from position i in the string.8. string_name.append(s2) appends s2 to the end of the string.9. string_name.replace(i, len, ss) replaces a segment of the string starting at position i with length len with string ss.10. string_name.find(subs[, pos]) finds the first occurrence of substring subs starting from position pos in the string. If not found, it returns -1. When pos is omitted, it defaults to searching from the beginning.11. string_name.substr(i, len) extracts a substring of length len starting from index i in the string.12. toupper(character_array_name), strupr(character_array_name) converts lowercase letters in the character array to uppercase letters.13. tolower(character_array_name), strlwr(character_array_name) converts uppercase letters in the character array to lowercase.14. string_name.empty() checks if the string is empty, returning 1 (true) if empty, otherwise returning 0 (false).
Programming is like learning to ride a bike; you may fall many times at first, but with practice, you will become more skilled! Don’t be afraid of encountering problems; it’s a necessary path for growth. Arrays are the foundational building blocks of the programming world; mastering them will enable you to construct more complex program structures.
C++ Collection:1. C++ Programming Beginner’s Preparation Guide2. Basic Knowledge of C++ Language3. C++ Input and Output4. Essential Vocabulary for C++ Beginners5. C++ Selection Structures6. C++ Loop Structures7. C++ Nested Loops8. C++ Arrays (Part I)