---
title: "判定字串"
author: "hufeng"
date: "2014-08-26T16:31:31Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2014-08-26T16:31:31Z"
id: 2109214
url: "https://hufeng.iteye.com/blog/2109214"
---

```c

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
/*
编一C程序，它能读入6个字符串（每个串长度都不大于80，可能含空格字符），
依次判断前5个串是否为第6个串的子串，若是则输出YES；否则输出NO。
*/

int substrcheck(char *son,char *father)
{
	int result =1,i,j;
	char *s,*f;
	int sl,fl;
	s=son,f=father;
	sl=strlen(son);
	fl=strlen(father);
	//比较的次数
	for(i=0;i<=fl-sl;i++)
	{
		result=1;
		for(j=0;j<sl;j++)
		{
		//	printf("s=%c f=%c\n",son[j],father[j+i]);
			if(son[j]!=father[j+i])
			{
				result = 0;
				break;
			}
		}
		//printf("\n\n");
		if(result==1)return result;
	}
	return result;
}

void main()
{
	char *str[6];
	int i;
	for(i=0;i<6;i++)
	{
		printf("请输入第%d个字符串:\n",i+1);
		str[i]=(char *)malloc(80*sizeof(char));
		gets(str[i]);//记得判定80长度
	}
	for(i=0;i<5;i++)
	{
		if(substrcheck(str[i],str[5])==0)printf("NO\n");
		else printf("YES\n");
	}
}


```