Implement a string search program (do not use built-in string functions)

Topic: Implement a string search program.
Requirement: Given a string s=princess-connect-redive, let the user find connect and judge whether connect is in the string s.
Do not use built-in string functions, please write your own program to implement a string search algorithm.


Problem-solving ideas: write a string search function

Look at the main function first
int main()
{
    
    
    	char s[]="princess-connect-redive";
    	char str0[]="connect";		
    	int res = strcompare(str0,s);
    	printf("大家好,这是字符串查找程序,如下:\n");
    	if(res)
    		printf("%s在%s中\n\n",str0,s);
    	else
			printf("%s不在%s中\n\n",str0,s);
		while(1) {
    
    strinput();}
		return 0;
}
The string s and str0 are the strings to be compared by the title; strcompare() is the string comparison function; strinput() is the string input function.

Key functions:

1. The function strlength to obtain the length of a string
/************************
函数:int strlength(char *s)
功能:返回输入字符串的长度
参数:字符串 char *s
返回值:int整型
*************************/
int strlength(char *s)
{
    
    
    int count = 0;;
	for(int i=0;i<100;i++)
	{
    
    
		if(s[i]=='\0')
			break;
		else
			count++;	
	}
	return count;
}


2. String search and comparison function strcompare

/************************
函数:int strcompare(char *s)
功能:输入字符串str1,str2,判断str1是否在str2中,
      是则返回1,否则返回0
参数:字符串 char *str1,char *str2
返回值:int整型(1或者0)
*************************/
int strcompare(char *str1,char *str2)
{
    
    
	int res,n,m;
	n = strlength(str1);
	m = strlength(str2);
	if(n>m)
		res = 0;
	else if(str1[0]=='\0')
		res = 0;
	else
	{
    
    
		int count = 0,i = 0;
		for(int j=0;j<m;j++)
		{
    
    
			if(str1[i]!='\0' || str2[j]!='\0')
				break;	 
A:			if(str1[i] == str2[j])
			{
    
    
				i++;
				j++;
				count++;
				goto A;
			}
			else
			{
    
    
				if(count==n)
					return res=1;
				else
				{
    
    
					if(j==m)
					{
    
    
						res = 0;
						break;
					}
					else
					{
    
    
						i = 0;
						count = 0;	 
					}
				}
			}
		}
	}
	return res;		
} 


3. The string input function strinput

/************************
函数:int strinput(void)
功能:从键盘输入两个字符串,判断要查找的字符串是否在
      被查找的字符串之中,并输出比较结果
参数:无
返回值:无
*************************/
void strinput(void)
{
    
    
	char str1[100],str2[100];
	printf("请输入bei查找的字符串:");
	gets(str2);
	printf("请输入yao查找的字符串:");
	gets(str1);
	int res = strcompare(str1,str2);
	if(res)
		printf("查找结果:%s在%s中\n\n",str1,str2);
	else
		printf("查找结果:%s不在%s中\n\n",str1,str2);
}


The complete code is as follows:

//字符串查找
#include <stdio.h>
 
int strlength(char *s);
int strcompare(char *str1,char *str2);
void strinput(void);
int main()
{
    
    
	char s[]="princess-connect-redive";
	char str0[]="connect";		
	int res = strcompare(str0,s);
	printf("大家好,这是字符串查找程序,如下:\n");
	if(res)
		printf("%s在%s中\n\n",str0,s);
	else
		printf("%s不在%s中\n\n",str0,s);
B:	strinput();
	goto B;
	return 0;
} 
 
//返回字符串的长度,要求字符串的长度不能超过100 
int strlength(char *s)
{
    
    
	int count = 0;;
	for(int i=0;i<100;i++)
	{
    
    
		if(s[i]=='\0')
			break;
		else
			count++;	
	}
	return count;
} 
 
//查找字符串str1是否在str2中,是则返回1,否则返回0 
int strcompare(char *str1,char *str2)
{
    
    
	int res,n,m;
	n = strlength(str1);
	m = strlength(str2);
	if(n>m)
		res = 0;
	else if(str1[0]=='\0')
		res = 0;
	else
	{
    
    
		int count = 0,i = 0;
		for(int j=0;j<m;j++)
		{
    
    
			if(str1[i]!='\0' || str2[j]!='\0')
				break;	//少了这个if检测最后一个字符就会出错! 
A:			if(str1[i] == str2[j])
			{
    
    
				i++;
				j++;
				count++;
				goto A;
			}
			else
			{
    
    
				if(count==n)
					return res=1;
				else
				{
    
    
					if(j==m)
					{
    
    
						res = 0;
						break;
					}
					else
					{
    
    
						i = 0;
						count = 0;	//count必须要复位! 
					}
				}
			}
		}
	}
	return res;		
} 
 
void strinput(void)
{
    
    
	char str1[100],str2[100];
	printf("请输入bei查找的字符串:");
	gets(str2);
	printf("请输入yao查找的字符串:");
	gets(str1);
	int res = strcompare(str1,str2);
//	printf("被查找的字符串的长度是%d\n",strlength(str2));
//	printf("要查找的字符串的长度是%d\n",strlength(str1));
	if(res)
		printf("查找结果:%s在%s中\n\n",str1,str2);
	else
		printf("查找结果:%s不在%s中\n\n",str1,str2);
}
 

The operation effect is as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46977029/article/details/109275609