Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. /************************************************************
  2.  * filename: mywc.c
  3.  *
  4.  * A naive implementation of wc.c
  5.  *
  6.  * Version: 1.0
  7.  * Author: Mark Fisher, CMP, UEA
  8.  * Date: 03 Nov. 2016
  9.  *
  10.  ************************************************************/
  11.  
  12. #include <stdio.h>
  13.  
  14. int main()
  15. {
  16.   int ch;
  17.   int linecount = 0;
  18.   int wordcount = 0;
  19.   int charcount = 0;
  20.   int flag = 0;
  21.  
  22.   //Repeat until End Of File character is reached
  23.   while ((ch = getchar()) != EOF) {
  24.     // Increment character count if NOT new line or space
  25.     if ((ch != ' ') && (ch != '\n')) {
  26.       charcount++;
  27.       flag = 0;
  28.     }
  29.     else {
  30.       if (flag==0) {
  31.         wordcount++;
  32.         flag = 1;
  33.       }
  34.       charcount++;
  35.     }
  36.  
  37.     // Increment line count if new line character
  38.     if (ch == '\n') { linecount++; }
  39.  
  40.   }
  41.  
  42.   if (flag==0) wordcount++;
  43.  
  44.   printf(" %d %d %d\n", linecount, wordcount, charcount);
  45.  
  46.   getchar(); // consume EOF
  47.  
  48.   return(0);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement