编写一个函数,从num个字符串中找出最长的一个字符串,并通过形参指针max传回改串地址,用C语言编写
时间: 2024-05-12 22:18:54 浏览: 83
```c
#include <stdio.h>
#include <string.h>
void findLongestStr(char *strList[], int num, char **max) {
*max = strList[0];
for(int i = 1; i < num; i++) {
if(strlen(strList[i]) > strlen(*max)) {
*max = strList[i];
}
}
}
int main() {
char *strList[] = {"abc", "defgh", "ijklmn", "opqrs", "tuv", "wxyz"};
char *maxStr;
findLongestStr(strList, 6, &maxStr);
printf("The longest string is %s\n", maxStr);
return 0;
}
```
相关问题
编写一个函数,从num个字符串中找出最长的一个字符串,并通过形参指针max传回改串地址
以下是Python代码实现:
def find_longest_string(strings, max):
longest_string = ""
for string in strings:
if len(string) > len(longest_string):
longest_string = string
max = longest_string
return max
# Example Usage
strings = ["hello", "world", "this", "is", "a", "test"]
max_string = ""
longest_string = find_longest_string(strings, max_string)
print("Longest string:", longest_string)
编写一个函数,从num个字符串中找出最长的一个字符串,并通过形参指针max传回该串地址
好的,这是一个编程问题,我可以回答。以下是函数的代码:
```c
void findLongestString(char** strings, int num, char** max) {
*max = strings[];
for (int i = 1; i < num; i++) {
if (strlen(strings[i]) > strlen(*max)) {
*max = strings[i];
}
}
}
```
这个函数接受一个字符串数组和数组长度作为参数,然后通过指针max返回最长的字符串的地址。
阅读全文
相关推荐
![](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)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)