> but when i compile this it is giving me an out put of 1
> even though i enter "strange#"
> should'nt it give an output of 8 instead of 1..
It increments the counter whenever whitespace is encountered. There isn't 8 whitespaces in "strange" is there? Besides, counter=0 after exection when strange#<newline> is input.
However,
Code:
char buf[200];
cin.getline(buf, 199);
unsigned int words=0, i=0, len=strlen(buf);
while(i<len&&buf[i]==' ')i++;
while(i<len){
words++;
while(i<len&&buf[i]!=' ')
i++;
while(i<len&&buf[i]==' ')i++;
}
//words is number of words in buf