在父字符串中查找子字符串(指针控制,也可选择标控制)
#pragma once #include<iostream> #include<assert.h> using namespace std; char* StrStr(char* source, char* dest) { assert(source&&dest); if (strlen(source) < strlen(dest)) return NULL; char* newSrc = NULL; char* newDest = dest; while (*source) { newSrc = source; while (*source&&*dest&&*source == *dest) { source++; dest++; } if (*dest == ‘\0‘) { return newSrc; } dest = newDest; source = newSrc + 1; } } void Test1() { char* src = "abcbcdef"; char* dest = "bcd"; cout << StrStr(src, dest) << endl; }
指针追踪截图
时间: 2024-08-02 11:02:27