字符串樸素匹配C++實現
/ 字符串的樸素匹配 通過每一個字母對應著主串 進行一次的進行比較,知道 其中的一個串的所有字母都匹配成功 */#include <iostream> #include <cstdio> #include <malloc.h> #include <cstring> using namespace std; int index(char *a, char *b) { int tarindex = 0; while(a[tarindex] != '\0') { int tarlen = tarindex; int patlen; for(patlen = 0; b[patlen] != 0; patlen++) { if(a[tarlen++] != b[patlen]) { break; } } if(b[patlen] == '\0') { return tarindex; } tarindex++; } return -1; } int main() { char *a; char *b; a = (char*)malloc(sizeof(char)); b = (char*)malloc(sizeof(char)); gets(a); gets(b); cout<<"第 "<<index(a, b) + 1<<" 個字母開始匹配!"<<endl; return 0; } </pre>
本文由用戶 bgn4 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!