Advertisement
chunkyguy

search string in string

Dec 22nd, 2010
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 KB | None | 0 0
  1. /*
  2.  author: sid
  3.  search arg 2 in arg 1
  4.  */
  5.  
  6. #include<stdio.h>
  7. #include<string.h>
  8.  
  9. int getword(FILE *f, char *word)
  10. {
  11.     int ch;
  12.     while(!isspace(ch = fgetc(f)) && ch != EOF)
  13.         *word++ = ch;
  14.     *word = '\0';
  15.     return ch;
  16. }
  17.  
  18. int main(int argc, char **argv)
  19. {
  20.     if(argc != 3)
  21.     {
  22.         printf("usage %s: <hay_string> <needle_string>\n",argv[0]);
  23.         return;
  24.     }
  25.    
  26.     char word[100];
  27.     int wc = 0;
  28.     int found = 0;
  29.     int i = 0;
  30.     for(; i < strlen(argv[1]); i++)
  31.     {
  32.         if(isspace(argv[1][i]))
  33.         {
  34.             word[wc] = '\0';
  35.             if(strstr(word,argv[2]))
  36.                 found++;
  37.             wc = 0;
  38.         }
  39.         word[wc++] = argv[1][i];
  40.     }
  41.    
  42.     printf("%s found %d times\n",argv[2],found);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement