frequency - Not counting spaces as words in c -
frequency - Not counting spaces as words in c -
#include <stdlib.h> #include <stdio.h> int main() { unsigned long c; unsigned long line; unsigned long word; char ch; c = 0; line = 0; word = 0; while((ch = getchar()) != eof) { c ++; if (ch == '\n') { line ++; } if (ch == ' ' || ch == '\n' || ch =='\'') { word ++; } } printf( "%lu %lu %lu\n", c, word, line ); homecoming 0; }
my programme works fine part, when add together spaces, counts spaces words. example, how you? counted 10 words, want count 3 words instead. how modify code work?
this 1 possible solution:
#include <stdlib.h> #include <stdio.h> int main() { unsigned long c; unsigned long line; unsigned long word; char ch; char lastch = -1; c = 0; line = 0; word = 0; while((ch = getchar()) != eof) { c ++; if (ch == '\n') { line ++; } if (ch == ' ' || ch == '\n' || ch =='\'') { if (!(lastch == ' ' && ch == ' ')) { word ++; } } lastch = ch; } printf( "%lu %lu %lu\n", c, word, line ); homecoming 0; }
hope helped, luck!
c frequency counting words letter
Comments
Post a Comment