Hi
Can one of you experts give me a hint with this. The code takes words typed in and counts them. My problem is that i want to modify it so the words are displayed one per line as in
this
is
some
text.
heres the code
any help would be appreciatedCode:#include <stdio.h> #include <conio.h> #include <ctype.h> #define true 1 #define false 0 #define kmaxlinelength 200 #define kzerobyte 0 void readline(char line[]); int countwords(char line[]); int main(void) { char line[kmaxlinelength]; int numwords; printf("please type a line of words\n"); readline(line); numwords=countwords(line); printf("\nthis line has %d word", numwords); if(numwords !=1) printf("s"); printf("\n%s\n", line); getch(); return 0; } void readline(char line[]) //this function reads the input { while((*line=getchar()) != '\n') line++; *line= kzerobyte; } int countwords(char line[])//this function counts words { int numwords, inword; numwords= 0; inword= false; while(*line != kzerobyte) { if(! isspace(*line)) { if(!inword) { numwords++; inword=true; } } else inword=false; /*i thought about entering a carriage return here though i don't know how exactly*/ line++; } return numwords; }![]()



LinkBack URL
About LinkBacks




