juinda

Untitled

Oct 11th, 2016
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. // 20161009平均字元長度.cpp : 定義主控台應用程式的進入點。
  2. //
  3.  
  4. #include "stdio.h"
  5.  
  6.  
  7. int main()
  8. {
  9. char a[100] = { '\0' };
  10. while (gets(a) != EOF)
  11. {
  12. float character = 0;
  13. float space = 1;
  14. int i=0;
  15.  
  16. while (a[i] == ' ')
  17. {
  18. i++;
  19. }
  20.  
  21. for (i; a[i]!='\0'; i++)
  22. {
  23. if (a[i] == ' ')
  24. {
  25. space++;
  26. i++;
  27. while (a[i] == ' ')
  28. {
  29. i++;
  30. }
  31. if (a[i] == '\0')
  32. {
  33. space--;
  34. break;
  35. }
  36. }
  37. if (a[i] != '\0'&&a[i] != ' ')
  38. character++;
  39. }
  40. printf("Average word length: ");
  41. printf("%.1f\n",character / space);
  42. char a[100] = { '\0' };
  43. }
  44.  
  45. return 0;
  46. }
Add Comment
Please, Sign In to add comment