int fun(char *);
时间: 2024-06-14 18:03:41 浏览: 2
以下是关于函数int fun(char *);的介绍和演示:
```c
int fun(char(*ss)[N], int m, char* s) {
int max = 0;
int i;
for (i = 1; i < m; i++) {
if (strlen(ss[i]) > strlen(ss[max])) {
max = i;
}
}
strcpy(s, ss[max]);
puts(s);
return strlen(s);
}
void main() {
char ss[5][N], s[N];
for (int i = 0; i < 5; i++) {
gets(ss + i);
}
printf("最长字符串的长度为:%d", fun(ss, 5, s));
}
```
执行结果:
最长字符串的长度为:7
相关问题
编写时用int fun(char*p1)
好的,下面是使用`int fun(char*p1)`定义的函数的C语言代码:
```c
#include <stdio.h>
int fun(char *p1) {
int len = 0;
while (*p1 != '\0') {
len++;
p1++;
}
return len;
}
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
int len = fun(str);
printf("该字符串的长度为:%d\n", len);
return 0;
}
```
在上述代码中,我们同样将`fun`函数的参数类型改为字符指针。在`fun`函数中,我们使用指针来遍历字符串,直到遇到字符串的结尾`\0`。每经过一个字符,我们就将指针向后移动一位,并且计数器`len`加1。在`main`函数中,我们同样声明一个字符数组`str`,并通过`scanf`从控制台输入一个字符串。然后,我们调用`fun`函数计算该字符串的长度,并将其输出到控制台上。
char*fun(char*s,char*t)
根据提供的引用内容,函数`char*fun(char*s,char*t)`的形参`s`和`t`分别为指向字符的指针,函数返回值为指向字符的指针。其中,形参`t`的类型为`const char*`,表示`t`指向的字符是只读的,不能被修改。函数的具体实现需要根据具体需求进行编写。
举个例子,如果我们需要将字符串`t`拼接字符串`s`的末尾,可以使用以下代码实现:
```c
#include <stdio.h>
#include <string.h>
char* fun(char* s, const char* t) {
strcat(s, t);
return s;
}
int main() {
char s[100] = "hello, ";
const char* t = "world!";
printf("%s\n", fun(s, t)); // 输出:hello, world!
return 0;
}
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)