C语言中常用的string一族函数有哪些?请用C语言代码实现
时间: 2023-12-11 22:02:47 浏览: 43
好的,我会遵守您的要求。C语言中常用的string一族函数有以下几个:
1. strlen:获取字符串长度
2. strcpy:复制字符串
3. strcat:拼接字符串
4. strcmp:比较字符串
5. strchr:查找字符在字符串中的位置
以下是C语言代码实现:
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
char str3[20];
// strlen
printf("The length of str1 is %d\n", strlen(str1));
// strcpy
strcpy(str3, str1);
printf("str3 after strcpy: %s\n", str3);
// strcat
strcat(str1, str2);
printf("str1 after strcat: %s\n", str1);
// strcmp
if (strcmp(str1, str2) == 0) {
printf("str1 and str2 are equal\n");
} else {
printf("str1 and str2 are not equal\n");
}
// strchr
char *ptr = strchr(str1, 'o');
printf("The first 'o' in str1 is at position %d\n", ptr - str1 + 1);
return 0;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)