Home >Backend Development >C#.Net Tutorial >Usage of string in c language
Strings in the C language are stored as null-terminated character arrays. Its characters can be accessed via subscripts, using strlen() to find the length. String operations include input/output, comparison, copying, concatenation, and searching. The sample code demonstrates operations such as access, length, copy, and search.
In C language, a string is a null-terminated character array.
char str[] = "Hello World";
str[i]
, where i is the index of the character in the string. strlen()
function. Input and output:
gets()
or scanf()
puts()
or printf()
Comparison:
strcmp(str1, str2) == 0
strcmp(str1, str2) != 0
##Copy:
Splicing:
Search:
<code class="c">#include <stdio.h> #include <string.h> int main() { char str[] = "Hello World"; // 访问字符串字符 printf("第一个字符:%c\n", str[0]); // 字符串长度 printf("字符串长度:%d\n", strlen(str)); // 字符串比较 if (strcmp(str, "Hello World") == 0) { printf("字符串相等\n"); } // 字符串复制 char copy[20]; strcpy(copy, str); // 字符串拼接 strcat(str, "! Welcome"); // 字符串搜索 char* pos = strchr(str, '!'); if (pos) { printf("感叹号的位置:%d\n", pos - str); } return 0; }</code>
<code>第一个字符:H 字符串长度:11 字符串相等 Hello World! Welcome 感叹号的位置:11</code>
The above is the detailed content of Usage of string in c language. For more information, please follow other related articles on the PHP Chinese website!