字串搜尋

Prerequisites:

目的字串 hdhelhelloWor

搜尋字串 hello\0

要在目的字串中找到搜尋字串的開始位置。

目的字串

h d h e l h e l l o W o r
^                        

搜尋字串

h e l l o 0              
^                        

目的字串 + 1

h d h e l h e l l o W o r
  ^                      

搜尋字串 + 1

h e l l o 0              
  ^                      

發現不一樣,指標退回。

目的字串 + 0

h d h e l h e l l o W o r
^                        

搜尋字串 + 0

h e l l o 0              
^                        

目的字串往右移動 + 1

h d h e l h e l l o W o r
  ^                      

搜尋字串 + 0

  h e l l o 0            
  ^                      

發現二邊指標的值不同。


目的字串往右移動一格 + 1

h d h e l h e l l o W o r
    ^                    

搜尋字串 + 0

    h e l l o 0          
    ^                    

發現與搜查字串第1個字元相同,開始比較

目的字串 + 1

h d h e l h e l l o W o r
      ^                  

搜尋字串 + 1

    h e l l o 0          
      ^                  

目的字串 + 1

h d h e l h e l l o W o r
        ^                

搜尋字串 + 1

    h e l l o 0          
        ^                

目的字串 + 1

h d h e l h e l l o W o r
          ^              

搜尋字串 + 1

    h e l l o 0          
          ^              

發現不一樣,退回最初一開始比較的地方

目的字串 - 3

h d h e l h e l l o W o r
    ^                    

搜尋字串 - 3

    h e l l o 0          
    ^                    

目的字串往右移,與搜尋字串不一致 + 1

h d h e l h e l l o W o r
      ^                  

搜尋字串 + 0

      h e l l o 0        
      ^                  

目的字串往右移,與搜尋字串不一致 + 1

h d h e l h e l l o W o r
        ^                

搜尋字串 + 0

        h e l l o 0      
        ^                

目的字串往右移,與搜尋字串一致,開始比對。 + 1

h d h e l h e l l o W o r
          ^              

搜尋字串 + 0

          h e l l o 0    
          ^              

目的字串 + 1

h d h e l h e l l o W o r
            ^            

搜尋字串 + 1

          h e l l o 0    
            ^            

目的字串 + 1

h d h e l h e l l o W o r
              ^          

搜尋字串 + 1

          h e l l o 0    
              ^          

目的字串 + 1

h d h e l h e l l o W o r
                ^        

搜尋字串 + 1

          h e l l o 0    
                ^        

目的字串 + 1

h d h e l h e l l o W o r
                  ^      

搜尋字串 + 1

          h e l l o 0    
                  ^      

目的字串 + 1

h d h e l h e l l o W o r
                    ^    

搜尋字串 + 1

          h e l l o 0    
                    ^    

比對到搜尋字串\0,代表已經搜尋到了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const char* mystrstr(const char *dest, const char *search) {
  //i為dest的索引
  size_t i = 0;
  //j為serch的索引
  size_t j = 0;
  //目的字串長度
  size_t dlen = strlen(dest);
  //搜尋字串長度
  size_t slen = strlen(search);
  
  //若i超過目的字串長度,代表都找不到
  //若j超過搜尋字串長度,代表找到
  while((i < dlen) && (j < slen)) {
    //目的字元與搜索字元相等
    if(dest[i] == search[j]) {
      i++;//指標往下個字元移動
      j++;//指標往下個字元移動
    } else {
      //不相等
      i = i - j; //指標移到最開始的位址
      j = 0; //搜尋字串的索引移到最前面
      i++ ; //目的字串指標往下一個位址移動
    }
  }
  //若j等於搜尋字串的長度代表找到
  if(j == slen) return dest + (i - j);
  return 0;//尋找失敗
}
int main() {
  const char* p1 = mystrstr("hdhelhelloWor", "hello") ;
  if(p1 != 0) cout << p1 << endl;
  return 0;
}
helloWor

results matching ""

    No results matching ""