Advertisement
MUstar

IoT C언어 0710 - EX12_3HD

Jul 15th, 2017
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  1. #include <stdio.h>
  2. //#include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char search(char *word, char *s_word);
  6.  
  7. int main(void)
  8. {
  9.     char word[100] , s_word[100];
  10.  
  11.     printf("문자 입력 : ");
  12.     fgets(word,sizeof(word),stdin); //문자 입력
  13.     while(1)
  14.     {
  15.         printf("검색할 문자열 입력 (종료는 end) : ");
  16.         fgets(s_word,sizeof(s_word),stdin); //검색어 입력
  17.         s_word[strlen(s_word)-1] = '\0';    //'\n'을 null로 변환
  18.         if(strcmp(s_word,"end")==0) break;  //end로 빙력시 프로그램종료.
  19.         search(word,s_word);                //검색으로
  20.     }
  21.  
  22.     return 0;
  23. }
  24.  
  25. char search(char *word, char *s_word)
  26. {
  27.     char *temp, w_temp[100];            //비교시 사용하는 char형함수
  28.     int cnt=0, size = strlen(s_word);   //비교시 사용하는 int형함수
  29.  
  30.     strcpy(w_temp,word);                //w_temp에 word값 저장
  31.     while(2)
  32.     {
  33.         temp = strstr(w_temp,s_word);
  34.         //w_temp에서 s_word와 일치하는 값이 있으면 그위치의 앞값을 제외한 나머지값을 temp에 저장
  35.         //없으면 null을 temp에 저장
  36.         if(temp==NULL) break; //temp값이 null이면 나가기
  37.         strcpy(w_temp,temp+size); //temp의 맨앞값부터 s_word의 자리수만큼 삭제한 값을 w_temp에 저장
  38.         cnt++;
  39.     }
  40.     printf("검색결과 : %d\n",cnt);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement