Advertisement
xeritt

Word count in string

Oct 25th, 2019
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #define N 256
  6. uint32_t word_count(uint8_t *mas) {
  7.     int word = 0, count = 0;
  8.     for (int i = 0; mas[i]!='\0'; i++) {
  9.         if (isspace(mas[i]) == 0) {
  10.             if (word == 0) {
  11.                 count++;
  12.                 printf("sym=[%c]\n", mas[i]);
  13.             }
  14.             word = 1;
  15.         } else {
  16.             word = 0;
  17.         }
  18.     }
  19.     return count;
  20. }
  21.  
  22. int main(int argc, char **argv) {
  23.     char str[N];
  24.     printf("Input string:\n");
  25.     fgets(str, N, stdin);        //read line
  26.     str[strlen(str) - 1] = '\0'; // delete new line symbol
  27.     printf("str = [%s]\n", str);
  28.     printf("word count: %d", word_count((uint8_t*)str));
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement