Friday, October 10, 2014

UVA 494 Kindergarten Counting Game

I couldn't succeed in this problem. Here's my code that could not get accepted. It's quite frustrating. Wish I knew what the private key was. I think I have to let go though, as this will drive me insane if I force myself to get my code accepted. Letting go in life is quite essential to healthy and peaceful living.

What I did here was to go through a line character by character. If the current character is a letter, then turn on the switch to 1. If it is not a letter turn off the switch to 0. When the switch switches from 0 to 1, then I increment the word count. After a line is processed, the code prints the word count. Doesn't work though. huhu.

#include "stdio.h"
#include "string.h"

int main() {
char line[100];
  int word_count, i;
  bool word_found;

while (fgets(line, 100, stdin)) {
    for (i = 0, word_found = 0, word_count = 0; line[i]; i += 1)
      if ((line[i] >= 65 && line[i] <= 90) || (line[i] >= 97 && line[i] <= 122)) {
        if (word_found == 0) {
          word_count += 1;
          word_found = 1;
        }
      }
      else {
        word_found = 0;
      }

      printf("%d\n", word_count);
}

return 0;
}

No comments:

Post a Comment