Guest User

Untitled

a guest
Dec 4th, 2016
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int WordsCounter(char* str);
  5.  
  6. int main()
  7. {
  8.     char str[100] = { 0 };
  9.     int wordCount = 0;
  10.  
  11.     puts("======== This program is Word Counter. ========");
  12.  
  13.     gets(str);
  14.  
  15.     wordCount = WordsCounter(str);
  16.  
  17.     printf("\n[Your string has '%d' words.]\n\n", wordCount);
  18.  
  19.     return 0;
  20.  
  21. }
  22.  
  23. int WordsCounter(char* str){
  24.     char strBuff[50] = { 0 };
  25.     int wordCnt = 0;
  26.  
  27.     for (int i = 0; i < sizeof(*str); i++) {
  28.         if (str[i] != '.')
  29.             strcat(strBuff, str[i]);
  30.         if (str[i] == ' ') {
  31.             printf("%s\n", strBuff);
  32.             for (int j=0;j<sizeof(strBuff);j++)
  33.                 strBuff[j] = '\0';
  34.             wordCnt += 1;
  35.         }
  36.     }
  37.     return wordCnt;
  38. }
Add Comment
Please, Sign In to add comment