C語言中的字符串函數可以通過包含頭文件<string.h>來使用。下面是幾個常用的字符串函數及其用法:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World";
int len = strlen(str);
printf("Length of string: %d\n", len);
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[10];
strcpy(str2, str1);
printf("Copied string: %s\n", str2);
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = " World";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "Hello";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else {
printf("Strings are not equal\n");
}
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World";
char *ptr = strchr(str, 'o');
printf("First occurrence of 'o' is found at position: %ld\n", ptr - str + 1);
return 0;
}
這只是一小部分常用的字符串函數,還有很多其他函數可以用于字符串操作。詳細的函數列表和用法可以查閱C語言的官方文檔或參考相關教程。