3、编写一个c语言程序使其能在devc++上运行并完成下面的题目:,输入两个英文句子,每个句子里英文单词之间用空格分隔,输出它们最长的公共单词。例如: There is a tree behind the house 。A big tree is cut down there .最长公共词是 tree(不是there)
时间: 2024-04-09 13:29:22 浏览: 101
您可以使用以下的C语言程序来实现输入两个英文句子,并输出它们最长的公共单词:
```c
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 100
char* findLongestCommonWord(char* sentence1, char* sentence2) {
char* longestWord = "";
char* word;
char* token1 = strtok(sentence1, " ");
while (token1 != NULL) {
char* token2 = strtok(sentence2, " ");
while (token2 != NULL) {
if (strcmp(token1, token2) == 0 && strlen(token1) > strlen(longestWord)) {
longestWord = token1;
}
token2 = strtok(NULL, " ");
}
token1 = strtok(NULL, " ");
}
return longestWord;
}
int main() {
char sentence1[MAX_LENGTH];
char sentence2[MAX_LENGTH];
printf("请输入第一个句子:");
fgets(sentence1, sizeof(sentence1), stdin);
printf("请输入第二个句子:");
fgets(sentence2, sizeof(sentence2), stdin);
// 去除换行符
sentence1[strcspn(sentence1, "\n")] = 0;
sentence2[strcspn(sentence2, "\n")] = 0;
char* longestCommonWord = findLongestCommonWord(sentence1, sentence2);
printf("最长公共单词是:%s\n", longestCommonWord);
return 0;
}
```
在Dev-C++中,您可以将上述代码粘贴到一个新的源文件中,然后编译并运行该程序。程序将要求您输入两个英文句子,并输出它们最长的公共单词。在您给出的示例中,最长公共单词是"tree"。
阅读全文