subString
#include <iostream> int subString(char* sSeek, char* sKey) { char* p = sSeek; while(*p != ‘\0‘) { int flag = 1; char* pKey = sKey; char* pTarget = p; while((*pKey != ‘\0‘) && (*pTarget!=‘\0‘)) { if(*pKey != *pTarget) { flag = 0; break; } //--------------------------------------- pKey++; pTarget++; } if(1 == flag) { puts(p); return (p-sSeek); } //--------------------------------------- p++; } return -1; } int main(int argc, char** argv) { char* sKey="人民"; char* sSeek="DVD人民的名义!"; int ret = subString(sSeek, sKey); if(ret>-1) { printf("第%d个字符开始,有", ret); } return 0; }
时间: 2024-12-08 12:30:18