Friday, July 31, 2009

Newbie in C prog.help me?

i do not know much bout C prog...i need to write a prog. that'll read a line of text (sentence), and count the number of words in it, then displaying the words."





Example:


the output-





Enter a sentence:


Roses are red, violets are blue.





The text has 6 words, those are:


Roses


are


red


violets


are


blue





i rili dont know how to do it...help...


thanks in advance!

Newbie in C prog.help me?
Try this





#include %26lt;stdio.h%26gt;





void main() {


char string[200];


char string2[200];


int i, j, words;


printf("Enter a sentence:\n");


gets(string);





for(i=0, j=0, words=0; string[i]!=0; i++) {


while(string[i]==' ') i++;


if(string[i]!=0 %26amp;%26amp; string[i]!=' ') {


string2[j++]='\n';


words++;


}


while(string[i]!=0 %26amp;%26amp; string[i]!=' ') {


string2[j]=string[i];


j++;


i++;


}


}


string2[j]=0;


printf("The text has %d words, those are:",words);


printf(string2);


}


No comments:

Post a Comment