YouTip LogoYouTip

C Strings

# C Strings In the C language, a string is actually a one-dimensional character array terminated by the null character . Therefore, is used to mark the end of a string. **Null character** (also known as the terminator, abbreviated NUL) is a control character with a numeric value of 0. is an escape character, which tells the compiler that this is not the character 0, but the null character. The following declaration and initialization creates a **** string. Since the null character is stored at the end of the array, the size of the character array is one more than the number of characters in the word ****. ```c char site = {'R', 'U', 'N', 'O', 'O', 'B', ''}; According to array initialization rules, you can write the above statement as: ```c char site[] = ""; The following is the memory representation of a string defined in C/C++: ![C/C++ String Representation](#) Actually, you don't need to place the null character at the end of a string constant. The C compiler automatically places at the end of the string when initializing the array. Let's try to output the string above: ## Example ```c #include int main() { char site = {'R', 'U', 'N', 'O', 'O', 'B', ''}; printf(": %sn", site); return 0; } When the above code is compiled and executed, it produces the following result: : There are a large number of functions for manipulating strings in C: | No. | Function & Purpose | | --- | --- | | 1 | **strcpy(s1, s2);** Copies string s2 into string s1. | | 2 | **strcat(s1, s2);** Concatenates string s2 to the end of string s1. | | 3 | **strlen(s1);** Returns the length of string s1. | | 4 | **strcmp(s1, s2);** Returns 0 if s1 and s2 are the same; less than 0 if s1s2. | | 5 | **strchr(s1, ch);** Returns a pointer to the first occurrence of character ch in string s1. | | 6 | **strstr(s1, s2);** Returns a pointer to the first occurrence of string s2 in string s1. | The following example uses some of the above functions: ## Example ```c #include #include int main() { char str1 = ""; char str2 = "google"; char str3; int len ; /* Copy str1 to str3 */ strcpy(str3, str1); printf("strcpy( str3, str1) : %sn", str3); /* Concatenate str1 and str2 */ strcat(str1, str2); printf("strcat( str1, str2): %sn", str1); /* Total length after concatenation */ len = strlen(str1); printf("strlen(str1) : %dn", len); return 0; } When the above code is compiled and executed, it produces the following result: strcpy( str3, str1) : strcat( str1, str2): tutorialgoogle strlen(str1) : 12 You can find more string-related functions in the C Standard Library. ## 7 Notes Write a Note 1. #0 San San mr.***ogyo@outlook.com 457 Functions covered in this section and their full English names strcmp: string compare strcat: string catenate strcpy: string copy strlen: string length strlwr: string lowercase strupr: string uppercase (javascript:;)San San mr.***ogyo@outlook.com 9 years ago (2017-11-06) 2. #0 Felix pho***xdai@foxmail.com 128 When initializing a string with the following input, special attention must be paid to : ```c char greeting = {'H', 'e', 'l', 'l', 'o', ''}; If is not added at the end of the character array, the output will be incorrect: ```c // Initialize string char greeting = { 'H', 'e', 'l', 'l', 'o' }; printf("Greeting message: %sn", greeting); Output: Greeting message: HelloUninitialized Memory?Intrusion7(?β•”?β•šβ•”β•” When using a variable-length array to initialize a string, the default ending is ```c char greeting[] = "Hello"; printf("Greeting message: %s, greeting[] Length: %dn", greeting, sizeof(greeting)); Output: Greeting message: Hello, greeting[] Length: 6 **Conclusion:** When specifying the size of a character array, add 1 to the number of characters in the original string. (javascript:;)Felix pho***xdai@foxmail.com 8 years ago (2018-07-11) 3. #0 Wang Ning Cao~~~ tom***owww13@163.com (#) 115 **Difference between strlen and sizeof:** strlen is a function, sizeof is an operator. Both return results of type size_t, i.e., unsigned int type. - sizeof calculates the size of a variable and is not affected by the character ; - strlen calculates the length of a string, using as the basis for length determination. > More reference content: > > > > [Wang Ning Cao~~~](javascript:;) Wang Ning Cao~~~ tom***owww13@163.com (#) 8 years ago (2018-07-30) 4. #0 Beginner Also Famous 277***4352@qq.com (https://bbs.csdn.net/topics/20311409
← React Component ApiReact Props β†’