Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(int argc, char *argv[]){
- int charCount, spaceCount, wordCount;
- charCount = spaceCount = wordCount = 0;
- int ch, och = 0; // EOF Flag From Input or Character
- FILE *inputFile = fopen(argv[1], "r");
- if(inputFile == 0){
- printf("\nCannot open a file which doesn't exist\n");
- exit(8); // not really sure why it's 8, it just worked... :)
- }
- int word = 0; // in or out of word
- while(1){
- ch = fgetc(inputFile);
- if(ch == EOF)
- break;
- if(ch == ' '){
- ++spaceCount;
- word = 0;
- }else{
- if(word == 0){
- ++wordCount;
- word = 1;
- }
- }
- ++charCount;
- }
- printf("This file has %d spaces; %d words; and %d chars.\n", spaceCount, wordCount, charCount);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement