My code is the following:
#include <stdio.h>
#define SIZE 10000000
int main() {
char c, word[SIZE];
int len = 0, i, count = 0, w = 0;
while ((c = getc(stdin)) != EOF) {
if (c != ' ') {
word[len] = c;
len++;
} else {
for (i = 0; i < len; i++) {
if ((word[i] >= 48 && word[i] <= 57) || (word[i] >= 65 && word[i]<= 90) || (word[i] >= 97 && word[i] <= 122))
count++;
if (count == len)
w++;
}
count = 0;
len = 0;
}
}
printf("%d", w);
return 0;
}
it counts a number of special words in a line. A special word is a word that contains A-Z or a-z or 0-9. if a word contains even just one other character it's not a special word anymore. So my algorithm for finding this word is counting a number of A-Z, a-z and 0-9 in the word and comparing it to the length of the word, if they match then it's the special word.
My code has some problems:
1) it doesn't care about the last word in the line as there is no ' '
(space) after the last word.
2) it does strange things (outputs wrong number) when there is more lines in the input.
what I want it to do is that write a number of special words in separate lines like this:
input:
dog cat23 banana
$money dollars 352
output:
3
2
how do I do that?
about the first problem I thought of writing if (c == EOF)
in the loop but it doesn't work. I would just use fgets
but each word may have at most 10000000 characters so if in one line several words have this much characters how can an array hold this much memory?