Commits

sunqiang committed 7183a8a

another version of ch1.5.3 ex 1.8

  • Participants
  • Parent commits 680515a

Comments (0)

Files changed (1)

File ch01/05/08.ex1.8/main.c

 {
     int blanks, tabs, newlines;
     int c;
-    int done = 0;
     int lastchar = 0;
 
     blanks = tabs = newlines = 0;
-
-    while (done == 0) {
-        c = getchar();
+    lastchar = '\n';
+    while ((c = getchar()) != EOF) {
         if (c == ' ')
             ++blanks;
         else if (c == '\t')
             ++tabs;
         else if (c == '\n')
             ++newlines;
-        else if (c == EOF) {
-            if (lastchar != '\n') {
-                ++newlines; 
-                /* this is a bit of a semantic stretch, but it copes
-                 * with implementations where a text file might not
-                 * end with a newline. Thanks to Jim Stad for pointing
-                 * this out. */
-            }
-            done = 1;
+        lastchar = c;
         }
-        lastchar = c;
-    }
+    if (lastchar != '\n')
+        ++newlines; 
+
     printf("Blanks: %d\nTabs: %d\nLines: %d\n", blanks, tabs, newlines);
+
     return 0;
 }