C语言-从给定的两个英文单词字符串s1和s2中找到包含s1和s2中最长且相同的英文单词

大作业一:完善如下程序,函数max()从给定的两个英文单词字符串s1和s2中找到包含s1和s2中最长且相同的英文单词,同一字母大小写视作不同字符且单词全部由英文字母组成,单词之间由一个或多个空白字符分隔。
#include
#include
char s1[] = ”This is a c programming test”;
char s2[] = “This is a test for c programming”;
max (char * s1, char
s2)
{
}
main()
{
clrscr(); //清屏函数
max(s1,s2);
}
*

这是上学期C语言课上lpj老师给我们布置的大作业,看起来简单却对二维数组,指针有一定了解。以下是我个人的代码:

#include
#include
#include
#include"fun.h"

void main()
{
	char s1[] = "Thisssssssss is C programming             textxxxxxxxxxxx";
	char s2[] = "Thisssssssss is a text for C programming  textxxxxxxxxxxx";
	max2(s1, s2);
	getchar(); getchar();
}

以上是主函数

#include

void max2(char *s1, char *s2)
{
	int i, b, c = 0, z = 0, j = 0, m = 0, f = 0;
	char p[20][30] = { 0 };       //p[m][j]---q[c][f]
	char q[20][30] = { 0 };
	char answer[99] = { 0 };
	for (i = 0; s1[i] != '\0'; i++)
	{
		if (s1[i] != ' ')
			p[m][j++] = s1[i];      //p[0][1]被s1的[i]赋值
		else
		{
			p[m][j] = '\0';
			m++;
			j = 0;  //s1切块
		}
	}
	for (i = 0; s2[i] != '\0'; i++)
	{
		if (s2[i] != ' ')
			q[c][f++] = s2[i];      //q[0][1]被s2的[i]赋值
		else
		{
			q[c][f] = '\0';
			c++;
			f = 0;  //s2切块
		}
	}
	for (i = 0; i < m + 1; i++)
	{
		for (b = 0; b < c + 1; b++)
		{                                    //if (strstr(q[c], p[i]) != NULL) {
			if (strcmp(p[i], q[b]) == 0)         //if same
			{
				//if (strcmp(p[i], answer) > 0) 
				if (strlen(p[i]) > strlen(answer))
				{                    //如果大的是p[i]  //p[i->m][j]--q[b->c][f]          
										  //for (k = 0; k < i; k++)
					strcpy(answer, p[i]);
					//answer[k] = p[k];	     //如果用strcpy(answer, p[i])会报错;
				}                             //把p[i]拷给answer	               
			}
		}
	}
	printf("the same word:");
	puts(answer);
}


以上是头文件

如果运行遇到了
’strcpy’: This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
请参考:
解决’strcpy’: This function or variable may be unsafe.

运行结果
C语言-从给定的两个英文单词字符串s1和s2中找到包含s1和s2中最长且相同的英文单词_第1张图片
如果帮到了你请个点赞吧!

你可能感兴趣的:(C)