Counting the words in a paragraph using C -
i wrote program in c count number of words in paragraph. on terminal screen, takes 1 sentence input , freezes. ideas why? i've tried other approaches , work. want know why 1 doesn't. here's code:
#include <stdio.h> #include <conio.h> int main(void) { int i, m=0, t=1; char x, a[100]; clrscr(); printf("type paragraph count words in it.\n"); while(t==1) { i=0; while(x!='\n') { x=getchar(); a[i]=x; i++; if((x==' ')||(x=='.')||(x=='?')||(x=='!')) m++; } if(a[0]=='\n') t=0; } printf("\nnumber of words = %i", m); getch(); return 0; }
edit: logic i've tried use here is: inner loop takes in words upto 100 characters. once \n character encountered, loop exited. outer loop there extend 100 character limit infinite number of characters(until outer loop exited). outer loop exited if \n character encountered in first loop after previous loop terminated \n. implies end paragraph, need type \n\n.
when exits inner while
loop, if a[0]
isn't \n
, t
stay 1, since x
hasn't changed since exited inner loop, won't re-enter it, stay stuck in outer loop.
Comments
Post a Comment