개발
C 언어 - 두고두고 쓰기 위한 문자열 search 함수
Swimming_Kim
2019. 1. 4. 13:01
예를 들어서 the biggest theater in our city is there~ 이런 문장이 있다고 하자. 이 문장에서 there이라는 단어의 위치를 알고 싶을 때 사용하는 함수이다.
the biggest theater in our city is there~
there there there(!!!)
the 부분까지는 탐색을 계속하다가, r부분에서 불일치를 발견하고, 다시 다음 단어로 건너갔다. 이러한 기능을 하는 함수이다. 아마도 라이브러리에 더 완벽한 것이 있을 테지만...
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 | char search_str(char * dic, char * word) { int loc = 0; int search_loc = 0; while (*dic) { if (*dic == *word) { while (*word) { if (*dic != *word) { word -= search_loc; loc += search_loc; search_loc = 0; break; } dic++; word++; search_loc++; if (*word == 0) { return loc; } } } dic++; loc++; } return -1; } | cs |