Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define SIZE 128
  4.  
  5. int inword(int flag, char ch)
  6. {
  7. if (flag == 0 && ch != ' ')
  8. return 0;
  9. else if (flag == 1 && ch != ' ')
  10. return 1;
  11. else if (flag == 1 && ch == ' ')
  12. return 2;
  13. }
  14.  
  15. int main()
  16. {
  17. char line[SIZE] = { 0 };
  18.  
  19. printf("Enter your text\n");
  20. fgets(line, SIZE, stdin);
  21.  
  22. if (line[strlen(line) - 1] == '\n')
  23. line[strlen(line) - 1] = '\0';
  24.  
  25. int i = 0, flag = 0, wordsize = 0;
  26. while (line[i])
  27. {
  28. switch (inword(flag, line[i]))
  29. {
  30. case 0:
  31. flag = 1;
  32. wordsize++;
  33. putchar(line[i]);
  34. break;
  35. case 1:
  36. wordsize++;
  37. putchar(line[i]);
  38. break;
  39. case 2:
  40. flag = 0;
  41. printf(" %d\n", wordsize);
  42. wordsize = 0;
  43. break;
  44. }
  45. i++;
  46. }
  47. if (flag == 1 && i == strlen(line))
  48. printf(" %d\n", wordsize);
  49.  
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement