编写c函数求一个字符串的长度、实现2个字符串连接与复制。
时间: 2024-06-14 19:05:31 浏览: 10
以下是一个C函数,用于求一个字符串的长度:
```c
#include <stdio.h>
int stringLength(char *str) {
int length = 0;
while (*str != '\0') {
length++;
str++;
}
return length;
}
int main() {
char str[] = "Hello, World!";
int length = stringLength(str);
printf("Length of the string: %d\n", length); // 输出:Length of the string: 13
return 0;
}
```
以下是一个C函数,用于实现两个字符串的连接:
```c
#include <stdio.h>
#include <string.h>
void stringConcat(char *str1, char *str2) {
strcat(str1, str2);
}
int main() {
char str1[20] = "Hello";
char str2[] = ", World!";
stringConcat(str1, str2);
printf("Concatenated string: %s\n", str1); // 输出:Concatenated string: Hello, World!
return 0;
}
```
以下是一个C函数,用于实现字符串的复制:
```c
#include <stdio.h>
#include <string.h>
void stringCopy(char *dest, char *src) {
strcpy(dest, src);
}
int main() {
char src[] = "Hello, World!";
char dest[20];
stringCopy(dest, src);
printf("Copied string: %s\n", dest); // 输出:Copied string: Hello, World!
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)