문자열 내에 하위 문자열을 찾는 할당 코드를 작성해야합니다. 여기 하위 문자열 찾기
내 코드입니다, 내가 코멘트를 추가 :이 방법을 테스트 할 때 이// the target is the substring that we want to find in the source string
// m is the length of the target, and n is the length of the source
int contains(char target[], int m, char source[], int n) {
int flag = 0; // the source originally does not contain the target
int i;
// go through each character of the source string
for(i = 0; i < n; i++) {
int targetIndex = 0;
int j;
// check if the preceding characters of the source string are a substring
// that matches the target string
for(j = i; j < n && targetIndex < m; j++) {
if(target[targetIndex] == source[j]) {
flag = 1;
targetIndex += 1;
}
else {
flag = 0; // a letter does not match
break;
}
}
}
return flag;
}
그래서, 난 항상 0
반환받을를 내가 이유를 이해할 수 없다.
int i = contains("potatoes", 8, "toes", 4);
을 입력하면 0
이됩니다.
일부 인쇄 문을 입력하여 어떤 문자와 일치하는지 확인하려고 시도했으며 첫 문자가 "t"
인 것 같습니다.
이것은 기본적으로'strstr()'이므로, 구현 예를 살펴 보시기 바랍니다. 지. glibc에있는 하나. –
이 코드를 수정 하시겠습니까? 또는 더 나은 방법을 원하십니까? –