C language-find the longest and same English word in s1 and s2 from the given two English word strings s1 and s2

Homework 1: Complete the following program. The function max() finds the longest and the same English words in s1 and s2 from the given two English word strings s1 and s2. The case of the same letter is regarded as different characters and words All are composed of English letters, and words are separated by one or more blank characters.
#include <stdio.h>
#include <string.h>
char s1[] = ”This is ac programming test”;
char s2[] = “This is a test for c programming”;
max (char * s1, char
s2 )
{ } main() { clrscr(); //Clear the screen function max(s1,s2); }





*

This is a big homework given to us by lpj in the C language class last semester. It looks simple but has a certain understanding of two-dimensional arrays and pointers. The following is my personal code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#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();
}

The above is the main function

#include<stdio.h>

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);
}


The above is the header file

如果运行遇到了
’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.

Run results
operation result
If it helps, please like it!

Guess you like

Origin blog.csdn.net/NikoHsu/article/details/105791763