string The function library provides functions for operating strings, including: string comparison functions (strcmp(), strncmp(), strcasecmp()) string copy functions (strcpy(), strncpy()) characters String connection functions (strcat(), strncat()) String search functions (strchr(), strstr()) String conversion functions (strtol(), strtof(), strcpy()) String formatting functions (sprintf() ), sscanf())
Usage of string function in C language
Question: What is the use of string function in C language?
Answer: The string function library provides functions for operating strings, including string comparison, copying, concatenation, search, conversion and formatting, etc.
Detailed description:
String comparison function:
String copy function:
String concatenation function:
String search function:
String conversion function:
sprintf(): Format data into a string
<code class="c">#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
// 比较字符串
int result = strcmp(str1, str2);
if (result == 0) {
printf("字符串相等\n");
} else if (result < 0) {
printf("str1 小于 str2\n");
} else {
printf("str1 大于 str2\n");
}
// 复制字符串
strcpy(str1, str2);
printf("str1 现在是 %s\n", str1);
// 连接字符串
strcat(str1, "C");
printf("str1 现在是 %s\n", str1);
// 搜索字符串
char *pos = strchr(str1, 'o');
if (pos != NULL) {
printf("字符 'o' 在字符串中\n");
}
// 转换字符串
int num = strtol(str2, NULL, 10);
printf("str2 转换为整数为 %d\n", num);
return 0;
}</code>
The above is the detailed content of Usage of string function in c language. For more information, please follow other related articles on the PHP Chinese website!