c++ - How can I enumerate text read from a file line by line, and find the line with the most characters and words in C? -
i'm writing program supposed read text file (fed program via command line), , enumerate , print out each line , provide information on number of words, characters, , lines, information on line has characters/words.
so far i've been able count number of words, characters, , lines having trouble finding way enumerate , print each line out , find lines have characters/words.
here code
#include <stdio.h> #define in 1 #define out 0 int main( ) { int line, word, character, state, place, flag maxw, maxc; state = out; line = word = character = 0; while( (place = getchar()) != eof ) { ++character; flag = in; printf("%c", place); if( place == '\n' ) { ++line; printf("%d: ", (line + 1)); flag = in; } if(flag == in) { } if( place == ' ' || place == '\n' || place == '\t' ) state = out; else if( state == out ) { state = in; ++word; } } printf("%d lines, %d words, & %d characters.\n", line, word, character); return 0; }
create local highest character count , current character count
int highestcharcount = 0; int currentcharcount = 0;
keep track of character count
++currentcharcount;
each loop check if character count higher highest, if is, change highest.
if(currentcharcount > highestcharcount){ highestcharcount = currentcharcount ; }
reset @ same time new line character.
if( place == '\n' ) { currentcharcount = 0; ++line; printf("%d: ", (line + 1)); flag = in; }
and that, end highest char number.
Comments
Post a Comment