#pragma warning(disable:4996)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 100
void main()
{
char s1[max], s2[max], * p;
char* maxword(char* s1, char* s2);
printf("请输入第一个英文句子:");
gets_s(s1);
printf("请输入第二个英文句子:");
gets_s(s2);
p = maxword(s1, s2);
if (p == NULL)
{
printf("这两句没有相同的单词!\n");
}
else
{
printf("这句话最长的单词是:%s\n", p);
}
}
char* maxword(char* s1, char* s2)
{
char comm[max];
char ch1[max], ch2[max];
int i, j, found, maxlen = 0;
while (*s1 != '\0')
{
while (*s1 == ' ')/*跳过空格*/
{
s1++;
}
for (i = 0; *s1 != ' ' && *s1 != '\0'; i++, s1++);
{
ch1[i] = *s1;/*找到s1的某个单词存入一个数组中*/
}
ch1[i] = '\0';
if (i > maxlen)
{
found = 0;
while (*s2 != '\0' && found == 0)/*在s2中找相同的单词
*found == 0, 意味着还未找到相同单词位置*/
{
while (*s2 == ' ')/*跳过空格*/
{
s2++;
}
for (j = 0; *s2 != ' ' && *s2 != '\0'; j++)
{
ch2[j] = *s2;
}
ch2[j] = '\0';
if (i == j)
{
if (strcmp(ch1, ch2) == 0)/*找到相同的单词*/
{
found = 1;
maxlen = j;
strcpy(comm, ch2);
}
}
}
}
if (maxlen == 0)
{
return NULL;
}
else
{
return comm;
}
}
}
查找两个英文句子中最长相同单词
最新推荐文章于 2024-07-06 11:13:36 发布