//對字符最容易忽視的是字符串結(jié)尾會(huì)默認(rèn)添加一個(gè)不可見的‘0\’
//因此用memcpy()函數(shù)對字符串復(fù)制時(shí)容器一定要多申請一個(gè)字節(jié)。
//如:char* p1="copy";要想把其copy到另一個(gè)空間中必須申請5Byte的空間:
//做法:char *p2=(char*)melloc(5);memcpy(p2,p1,4);;p2[5]='0\';至此完成復(fù)制;
//因此用memcpy()函數(shù)對字符串復(fù)制時(shí)容器一定要多申請一個(gè)字節(jié)。
//如:char* p1="copy";要想把其copy到另一個(gè)空間中必須申請5Byte的空間:
//做法:char *p2=(char*)melloc(5);memcpy(p2,p1,4);;p2[5]='0\';至此完成復(fù)制;
#include "stdafx.h"
#include "string.h"
#include "malloc.h"
{
int count=0;
int len1=strlen(sub);
char *buffer=(char *)malloc(len1+1);//中間量內(nèi)存的申請
while(*(all+len1-1)!='\0')
{
memcpy(buffer,all,len1);//截取,存入buffer
buffer[len1]='\0';
if(strcmp(buffer,sub)==0)
{
count++;
all+=len1;
//all++;
}
else
{
all++;
}
}
free(buffer);
return count;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *a="aaaabaaaaaabaa";
char *b="aba";
int len=Findsub(a,b);
printf("------------%d\n",len);
return 0;
}