2012-05-24 3 views
2

안녕하세요.이 코드는 Morris-Pratt 알고리즘을 구현하려고 시도한 코드 중 하나입니다. 내 변수 "Temp"중 하나가 배열의 끝에 추가 문자를 가져 오기 때문에 변수가 일치하지 않는다고 판단하면 변수를 비교할 때입니다. 당신이 5에서 볼 수 있듯이 여기문자 배열에 여분의 문자가 출력되는 경우

SEARCHLEN: 8 
TEST:  athsoutg5?h 
SEARCH:  brilling 
-1 

...

 // Calculate the next talbe 
     char test[searchLen]; 

     for(int i = 0; i < searchLen; i++) 
     { 
      test[i] = fileContent[currPos+i]; 
     } 

     cout << "SEARCHLEN: " << searchLen << endl; 
     cout << "TEST: " << '\t' << '\t' << test << endl; 
     cout << "SEARCH: " << '\t' << search << endl; 
     cout << strcmp(test,search) << endl << endl; 
     // Determine if a match is detected 

     if(strcmp(test,search)==0) 
     { 
      cout << "----------------------> Match detected at: " << currPos << endl; 
     } 

     currPos ++; 
    } 

    return numberOfComparisons; 
} 

출력은 다음과 같습니다 ... 내 코드는? H는가 안되고 내 코드를 파괴한다.

답변

6

널 종결자를 추가해야합니다.

char test[searchLen + 1]; 
    test[searchLen] = '\0'; 
+0

AHhh! 그게 고마워. –

1

문자열이 \ 0으로 끝나지 않은 것처럼 보입니다. 아마도 복사/저장하는 것을 잊었을 것입니다.

관련 문제