分析一下#include<stdio.h> #include <string.h> int main(void) { char str[100][100]; char x[100],solve[100]; int len,max,lenth; int i = 0,j,k; while(strcmp(gets(str[i]),"stop") != 0){ i++; } i = 0; while(strcmp(str[i],"stop") != 0){ lenth = 0; max = 0; j = 0; while(str[i][j] != '\0'){ k = 0; while(str[i][j] != '\0' && str[i][j] != ' '){ x[k] = str[i][j]; k++; j++; } x[k] = '\0'; len = strlen(x); if(len > max){ max = len; strcpy(solve,x); } lenth += len; j++; } printf("%d %s\n",lenth,solve); i++; } return 0; }
时间: 2024-02-14 14:32:58 浏览: 23
这段代码是一个 C 语言程序,它的功能是读入一些字符串,对于每个字符串,找到其中最长的单词并输出该单词以及字符串中所有单词长度之和。
该程序使用了两个头文件,分别是<stdio.h>和<string.h>,前者提供了输入输出函数,后者提供了字符串处理函数。接着定义了一个字符数组str用来存储多个字符串。同时,定义了一个字符数组x和一个字符串solve,用来存储当前字符串中的单词和最长的单词。
程序通过一个while循环读入多个字符串,直到输入"stop"为止。在读入每个字符串时,程序会逐个字符扫描字符串,并将其中的单词存储到字符数组x中。当扫描到一个单词时,程序会计算该单词的长度,并判断该单词是否是当前字符串中最长的单词。最后程序会输出该字符串中所有单词的长度之和以及最长的单词。
值得注意的是,该程序使用了不安全的输入函数gets(),因为它无法判断输入的字符串是否超长,建议使用更安全的输入函数fgets()来代替。
相关问题
#include <stdio.h> void StrReverse ( char str[] ); int main() { char strArr[100]; gets(strArr); StrReverse(strArr); puts(strArr); return 0; }
这是一个反转字符串的程序,使用了 `gets` 函数获取输入字符串,调用了 `StrReverse` 函数对字符串进行反转,最后使用 `puts` 函数输出反转后的字符串。具体实现代码如下:
```
#include <stdio.h>
#include <string.h>
void StrReverse(char str[]) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
int main() {
char strArr[100];
printf("Enter a string: ");
gets(strArr);
StrReverse(strArr);
printf("Reversed string: %s\n", strArr);
return 0;
}
```
需要注意的是,`gets` 函数已经被标记为不安全,不建议使用。可以使用 `fgets` 函数代替,具体用法可以参考 C 语言标准库。
*定义函数实现n个字符串按字典顺序排序 开头: #include<stdio.h> #include<string.h> void fun (char *str[],int n)
好的,以下是一个实现n个字符串按字典顺序排序的示例代码:
```
#include <stdio.h>
#include <string.h>
void fun(char *str[], int n) {
int i, j;
char *temp;
// 使用冒泡排序算法
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (strcmp(str[j], str[j+1]) > 0) {
// 交换两个字符串的位置
temp = str[j];
str[j] = str[j+1];
str[j+1] = temp;
}
}
}
}
int main() {
char *str[] = {"apple", "banana", "orange", "grape", "kiwi"};
int n = sizeof(str) / sizeof(str[0]);
fun(str, n);
printf("排序后的字符串数组:\n");
for (int i = 0; i < n; i++) {
printf("%s ", str[i]);
}
printf("\n");
return 0;
}
```
在这个示例代码中,我们使用了冒泡排序算法对字符串数组进行排序。在排序过程中,我们使用了 `strcmp()` 函数来比较两个字符串的大小关系,并使用指针变量 `temp` 来交换两个字符串在数组中的位置。最后,我们输出排好序的字符串数组。
请注意,这个示例代码中的字符串数组是在程序中直接定义的。如果您需要从用户输入中获取字符串数据,请使用 `scanf()` 或其他输入函数来读取数据,并将它们存储在字符串数组中。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)