#include
#include
int main(){
char str[100],s[100],i,j,flag=0;
printf("请输入主串:\n");
gets(str);
printf("请输入字串:\n");
gets(s);
i=0,j=0;
while(iif(str[i] == s[j]){//如果字符相同则两个字符都增加
i++;
j++;
}
else{
i=i-j+1; //主串字符回到比较最开始比较的后一个字符
j=0; //字串字符重新开始
}
if(j == strlen(s)){//如果匹配成功
flag=1; //字串出现
break;
}
}
if(flag==1) printf("%s中包含%s\n",str,s);
else printf("%s中不包含%s\n",str,s);
return 0;
}
#include "windows.h"
#include "stdio.h"
bool FindString(char* pStr, char* pSub)
{
bool bRet = false;
int nStrLen = strlen(pStr);
int nSubLen = strlen(pSub);
for(int i = 0; i < nStrLen; i++)
{
int j = 0;
for(j = 0; j < nSubLen; j++)
{
if(pStr[i + j] != pSub[j])
{
break;
}
}
if(j >= nSubLen)
{
bRet = true;
break;
}
}
return bRet;
}
void main()
{
char *pStr = "Those were such happy times";
char *pSub = "happy";
printf("字符串:%s\n", pStr);
printf("子符串:%s\n", pSub);
printf("%s", FindString(pStr, pSub) ? "存在\n":"不存在\n");
system("pause");
return ;
}