65,210
社区成员
发帖
与我相关
我的任务
分享/* publicdef.h*/
#ifndef _PUBLIC_DEF_H
#define _PUBLIC_DEF_H
#define B_TRUE true
#define B_FALSE false
typedef char CHAR;
typedef unsigned short USHORT;
typedef unsigned long ULONG;
typedef bool BOOL;
#define ASSERT(_bResult)\
{\
if (!_bResult)\
{\
printf("%s(%d)\n", __FILE__, __LINE__);\
}\
}\
BOOL Is_String_Contain(CHAR* pcStrMother, CHAR* pcStrChild);
#endif/* main.cpp */
#include <stdio.h>
#include <string.h>
#include "publicdef.h"
BOOL Is_String_Contain(CHAR* pcStrMother, CHAR* pcStrChild)
{
ULONG ulMotherLen = 0;
ULONG ulChildLen = 0;
ULONG ulStartPos = 0;
ULONG ulCnt = 0;
ULONG ulMatchCount = 0;
if (NULL == pcStrMother || NULL == pcStrChild)
{
ASSERT(0);
return B_FALSE;
}
ulMotherLen = (ULONG)strlen(pcStrMother);
ulChildLen = (ULONG)strlen(pcStrChild);
if (ulMotherLen < ulChildLen)
{
return B_FALSE;
}
while (ulStartPos + ulChildLen <= ulMotherLen)
{
for (ulCnt = 0; ulCnt < ulChildLen ; ulCnt ++)
{
if(pcStrChild[ulCnt] == pcStrMother[ulStartPos + ulCnt])
{
ulMatchCount++;
}
else
{
break;
}
}
if(ulMatchCount == ulChildLen)
{
return B_TRUE;
}
ulMatchCount = 0;
ulStartPos++;
}
return B_FALSE;
}
int main()
{
CHAR* str1 = "abc";
CHAR* str2 = "bc";
if (B_TRUE == Is_String_Contain(str1, str2))
{
printf("contained");
}
else
{
printf("Not contained");
}
return 0;
}
//这里的
for (ulCnt = 0; ulCnt < ulChildLen ; ulCnt ++)
{
if(pcStrChild[ulCnt] == pcStrMother[ulStartPos + ulCnt])
{
ulMatchCount++;
}
else
{
break;
}
}
//我会写成
for (ulCnt = 0; ulCnt < ulChildLen ; ulCnt ++)
{
if(pcStrChild[ulCnt] != pcStrMother[ulStartPos + ulCnt])
break;
ulMatchCount++;
}