In the C language, a string is actually a one-dimensional array of characters terminated by a null character \0. Therefore, \0 is used to mark the end of the string.The null character, also known as the terminator, abbreviated as NUL, is a control character with a value of 0. The \0 is an escape character that tells the compiler that this is not the character 0, but the null character.The following declaration and initialization create a PLC string. Since the null character \0 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 PLC.
char site[4] = {'P', 'L', 'C', '\0'};
According to the array initialization rules, you can write the above statement as follows:
char site[] = "PLC";
You do not need to place the \0 character at the end of the string constant. The C compiler automatically adds \0 at the end of the string when initializing the array. Let’s try to output the above string:
#include <stdio.h>
int main (){
char site[4] = {'P', 'L', 'C', '\0'};
char site1[]="PLC"; // Automatically includes '\0', length is 4
printf("Length of site array: %d\n", sizeof(site)/sizeof(site[0]));
printf("Length of site1 array: %d", sizeof(site1)/sizeof(site1[0]));
return 0;
}
The final compilation and execution result is:
String FunctionsBefore using these strings, we need to include the header file
#include<string.h> // Include string header file
To facilitate the use of strings, I have listed common string functions:
| Function | Purpose | Usage Example |
|---|---|---|
| strlen | Calculate string length (excluding <span>\0</span>) |
<span>size_t n = strlen("hello"); // n == 5</span> |
| strcpy | Copy string (including <span>\0</span>) |
<span>char buf[10]; strcpy(buf, "abc");</span> |
| strncpy | Copy up to <span>n</span> characters, does not guarantee <span>\0</span> |
<span>strncpy(buf, "abc", sizeof(buf)-1); buf[sizeof(buf)-1]='\0';</span> |
| strcat | Append string | <span>char buf[20]="hi"; strcat(buf, " world");</span> |
| strncat | Append up to <span>n</span> characters, automatically adds <span>\0</span> |
<span>strncat(buf, " world", 3); // "hi wor"</span> |
| strcmp | Compare two strings lexicographically | <span>if (strcmp(a, b) == 0) puts("equal");</span> |
| strncmp | Compare the first <span>n</span> characters |
<span>strncmp("abc", "abd", 2) == 0; // true</span> |
| strchr | Find the first occurrence of a character | <span>char *p = strchr("hello", 'l'); // p -> "llo"</span> |
| strrchr | Find the last occurrence of a character | <span>char *p = strrchr("hello", 'l'); // p -> "lo"</span> |
| strstr | Find the first occurrence of a substring | <span>char *p = strstr("banana", "nan"); // p -> "nana"</span> |
| strtok | Split string by delimiter | <span>strtok("a,b,c", ","); First returns </span><code><span>"a"</span> |
| sprintf | Format output to string | <span>char buf[20]; sprintf(buf, "%d+%d=%d", 2, 3, 5);</span> |
| snprintf | Safe version of <span>sprintf</span>, prevents overflow |
<span>snprintf(buf, sizeof(buf), "%.2f", 3.14159);</span> |
Of course, if you need the complete documentation, here is an example with DEV-CPP, you can hold down the ctrl key and left-click on the string header file to view the specific complete functions, like this:
If you are not comfortable browsing in English, you can search for the corresponding Chinese API documentation online.Example
#include <stdio.h>
#include <string.h>
int main (){
char str1[14] = "plc";
char str2[14] = "google";
char str3[14];
int len ;
/* Copy str1 to str3 */
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3);
/* Concatenate str1 and str2 */
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1);
/* Total length of str1 after concatenation */
len = strlen(str1);
printf("strlen(str1) : %d\n", len);
return 0;
}
The compilation and execution result is as follows:
Well, that concludes the concepts related to strings. You can practice the corresponding functions more, as they will be useful whether you participate in competitions or work (of course, they are used more in competitions
). In the next lesson, we will learn about structures in C language, so you can familiarize yourself with it in advance (づ。◕‿‿◕。)づ