2
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

//function prototypes
void checkAnswer(char *, char[]);
int main(void) {
  char *strGame[5] = { "ADELANGUAGEFERVZOPIBMOU", "ZBPOINTERSKLMLOOPMNOCOT",
      "PODSTRINGGDIWHIEEICERLS", "YVCPROGRAMMERWQKNULTHMD",
      "UKUNIXFIMWXIZEQZINPUTEX" };
  char answer[80] = { 0 };
  int displayed = 0;
  int x;
  int startTime = 0;
  system("clear");
  printf("\n\n\tWord Find\n\n");
  startTime = time(NULL);
  for (x = 0; x < 5; x++) {
    /* DISPLAY TEXT FOR A FEW SECONDS */
    while (startTime + 3 > time(NULL)) {
      if (displayed == 0) {
        printf("\nFind a word in: \n\n");
        printf("%s\n\n", strGame[x]);
        displayed = 1;
      }
    }
    system("clear");
    printf("\nEnter word found: ");
    fgets(answer, 80, stdin);
    checkAnswer(strGame[x], answer);
    displayed = 0;
    startTime = time(NULL);
  }
}
void checkAnswer(char *string1, char string2[]) {
  int x;

  for (x = 0; x <= strlen(string2); x++)
    string2[x] = toupper(string2[x]);
  if (strstr(string1, string2) != 0)
    printf("\nGreat job!\n");
  else
    printf("\nSorry, word not found!\n");

}

When I run the code, it doesn't register my input correctly. It tells me that the word wasn't found. I used toupper to make my input the same as my strings and strstr to compare my input with the strings. I took this from a basic C programming book. It used gets. I know that you shouldn't use gets so I changed it to fgets. Is this where the problem is? Any suggestions?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 2
    to be in the last newline('\n') in the string that you entered in fgets does not take into account. You must remove the newline. – BLUEPIXY Jul 06 '14 at 13:24
  • regarding the line: for (x = 0; x <= strlen(string2); x++), this line is one char too long. so it should be: for (x = 0; x < strlen(string2); x++) as there is no need to toupper('\n') – user3629249 Jul 07 '14 at 01:37
  • you need to remove the new line char(s) from the end of the user string, as suggested by others. – user3629249 Jul 07 '14 at 01:38
  • regarding the line: if (strstr(string1, string2) != 0), strstr returns a pointer or NULL, not 0, so the line should be: if (strstr(string1, string2) != NULL) – user3629249 Jul 07 '14 at 01:42
  • the '\n' needs to be replaced with '\0' so the read string is still properly terminated. And get the strlen value AFTER replacing the newline with a null char. – user3629249 Jul 07 '14 at 01:47

2 Answers2

0

You can avoid the issue with the \n (newline) mentioned by BLUEPIXY -- namely, that gets() removes it but fgets() does not -- by reversing the terms in your call to checkAnswer():

checkAnswer(answer, strGame[x]);

checkAnswer() then uses the same order with strstr(). If you search for "foobar" in "foobar\n", strstr() will return a pointer. But if you search for "foobar\n" in "foobar", it won't.

The newline is there because the user hits Enter. So another way around this would be to add a \n to the end of all your strGame[] strings. Or, you could remove any newline in the answer with:

void truncateAtNewline (char *str) {
    char *p = strchr(str, '\n');
    if (p) *p = '\0';
}
CodeClown42
  • 11,194
  • 1
  • 32
  • 67
  • 1
    Yes, because a gets() does not account for the size of the input buffer. Buffer of large size may make your program either crash or produce undefined behavior. – darxtrix Jul 06 '14 at 13:42
  • @black_perl I'm not recommending `gets()`. The OP is right to use `fgets()` instead, but it creates a minor issue with the newline that must be taken into account and dealt with. – CodeClown42 Jul 06 '14 at 13:44
  • No problem. If you think you have an answer to your question, tick the big checkmark at the top of the answer to indicate this. – CodeClown42 Jul 07 '14 at 14:32
0

The problem is that fgets() will leave the newline at the end of the string. When you type the word, then you press Enter and fgets() interprets that as input.

So, a way to bypass this is to eat the newline by doing this:

fgets(answer, 80, stdin);
// go to the last position where the
// newline is placed and replace it
// with the null terminator
answer[strlen(answer)-1] = '\0';

Also here:

 for (x = 0; x <= strlen(string2); x++)
    string2[x] = toupper(string2[x]);

the <= is not needed, since you start from 0, thus change it to this:

 for (x = 0; x < strlen(string2); x++)
    string2[x] = toupper(string2[x]);

How I found your problem? I used printf to output the strings before comparing them.

void checkAnswer(char *string1, char string2[]) {
  int x;

  for (x = 0; x < strlen(string2); x++)
    string2[x] = toupper(string2[x]);
  printf("|%s|\n", string1);
  printf("|%s|\n", string2);
  if (strstr(string1, string2) != 0)
    printf("\nGreat job!\n");
  else
    printf("\nSorry, word not found!\n");

}

Output before my fix:

|ADELANGUAGEFERVZOPIBMOU|
|ADEL
|

Output after the fix:

|ADELANGUAGEFERVZOPIBMOU|
|ADEL|

Or you can use a function to trim newlines and spaces. I have some methods here.

Consider also not using system().

Moreover, always add a return 0; line of code before your main() ends.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305