Advertisement
Nusrat_Ullah

Count Articles of a string.

Mar 26th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. int cnt;
  4. char str[10000];
  5. void article_count(char *article)
  6. {
  7.     int article_len=strlen(article);
  8.     char *tmp_str=str;                               //we put our main string in another temporary string
  9.     while(tmp_str=strstr(tmp_str,article{            //strstr will return first occurrence from any position to article,
  10.                                                      //if not found it will return null and loop will break
  11.         cnt++;                                       //if article found, increment counter variable cnt
  12.         tmp_str+=article_len;                        //Already found the article, so we can skip that article length ,
  13.                                                      //and search again from that position to next article occurrence
  14.     }
  15. }
  16. int main()
  17. {
  18.     scanf("%[^\n]s",str);
  19.     cnt=0;
  20.     article_count("A ");
  21.     article_count(" a ");
  22.     article_count("An ");
  23.     article_count(" an ");
  24.     article_count("The ");
  25.     article_count(" the ");
  26.     printf("%d\n",cnt);
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement