Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- //#include <stdlib.h>
- #include <string.h>
- char search(char *word, char *s_word);
- int main(void)
- {
- char word[100] , s_word[100];
- printf("문자 입력 : ");
- fgets(word,sizeof(word),stdin); //문자 입력
- while(1)
- {
- printf("검색할 문자열 입력 (종료는 end) : ");
- fgets(s_word,sizeof(s_word),stdin); //검색어 입력
- s_word[strlen(s_word)-1] = '\0'; //'\n'을 null로 변환
- if(strcmp(s_word,"end")==0) break; //end로 빙력시 프로그램종료.
- search(word,s_word); //검색으로
- }
- return 0;
- }
- char search(char *word, char *s_word)
- {
- char *temp, w_temp[100]; //비교시 사용하는 char형함수
- int cnt=0, size = strlen(s_word); //비교시 사용하는 int형함수
- strcpy(w_temp,word); //w_temp에 word값 저장
- while(2)
- {
- temp = strstr(w_temp,s_word);
- //w_temp에서 s_word와 일치하는 값이 있으면 그위치의 앞값을 제외한 나머지값을 temp에 저장
- //없으면 null을 temp에 저장
- if(temp==NULL) break; //temp값이 null이면 나가기
- strcpy(w_temp,temp+size); //temp의 맨앞값부터 s_word의 자리수만큼 삭제한 값을 w_temp에 저장
- cnt++;
- }
- printf("검색결과 : %d\n",cnt);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement