Write a Program How many Words in a File C Programming Home » C File Input/Output » How many Words in a File #include #define MAX_LEN 1024 int main() { /*Read the file.*/ char ch; int char_count = 0, word_count = 0, line_count = 0; int in_word = 0; char file_name[MAX_LEN]; FILE *fp; printf("Enter a file name: "); scanf("%s", file_name); fp = fopen(file_name, "r"); if(fp == NULL) { printf("Could not open the file %s\n", file_name); return 1; } while ((ch = fgetc(fp)) != EOF) { char_count++; if(ch == ' ' || ch == '\t' || ch == '\0' || ch == '\n') { if (in_word) { in_word = 0; word_count++; } if(ch = '\0' || ch == '\n') line_count++; } else { in_word = 1; } } printf("In the file %s:\n", file_name); printf("Number of characters: %d.\n", char_count); printf("Number of words: %d.\n", word_count); printf("Number of lines: %d.\n", line_count); getch() ; return 0; } Tweet Share Share Share Share Related Post
3 comments
Click here for commentsIf You are looking for java programs we have it at our Blog Please have a look https://letml.blogspot.com/2019/02/inheritancejavaoopconcepts.html
Replyyou have a great content https://seoujjain.blogspot.com/ do visit our blog
ReplyGreat program.
ReplyFor more information on C programming do visit https://ekeeda.com/
ConversionConversion EmoticonEmoticon