Re: good algorithms come with practice and reading good code/books?
- From: "vlsidesign" <fordgwf@xxxxxxxxx>
- Date: 30 Dec 2006 12:13:52 -0800
August Karlstrom wrote:
Anyway, here is my version of the program:Cool. Thanks for sharing it :)
#include <ctype.h>Thanks I was unaware of isspace function.
#include <stdio.h>
int main(void)
{
int c, words = 0, chars = 0, insideword = 0;
c = getchar();
while (c != EOF) {
if (isspace(c)) {
if (insideword) { words++; }I like the way you did it and yours works, my didn't. I was doing it
insideword = 0;
like this
if (nc > 0) ++nw;
nc = 0;
My version here did take care of multiple spaces in a row because nc
(number of characters in word) is only > 0 if it is the first
whitespace char (coming out of word, and nc is not set to 0 yet), but
it incorrectly incremented nw (new word) when first character of input
was a whitespace.
} else {This catches when the last char is nonwhitespace and then EOF. My
chars++;
insideword = 1;
}
c = getchar();
}
if (insideword) { words++; }
version I didn't catch this.
printf("Found %d words and %d non-whitespace characters\n",Thanks again for sharing that.
words, chars);
return 0;
}
August
.
- References:
- good algorithms come with practice and reading good code/books?
- From: vlsidesign
- Re: good algorithms come with practice and reading good code/books?
- From: August Karlstrom
- good algorithms come with practice and reading good code/books?
- Prev by Date: Re: MNC Hyderabad is Looking for C++ Developer...
- Next by Date: Re: good algorithms come with practice and reading good code/books?
- Previous by thread: Re: good algorithms come with practice and reading good code/books?
- Next by thread: Re: good algorithms come with practice and reading good code/books?
- Index(es):