1

I am writing a code which compares two strings for a simple guessing game. One string is pre-set, in my code it's "Eggman", and this is then compared with a user input, if the letter in the same position is the same then the user is shown the letter on screen, if it is not the same as the pre-set string then a question mark appears. For example, since the pre-set string is "Eggman" the user will be asked for an input, if the input is for example "Eclair", then the program outputs "E?????", which is exactly what I want the program to do, however, I have used multiple if statements, which has made the code inconvenient, and cannot really think of a way to do it with iteration. I thought about using the strcmp() function, but I pretty sure that it can't be used to compare singular characters in a string. The code is below:

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void checkAnswer();

int main()
{
    checkAnswer();
    return 0;
}
void checkAnswer()
{
    char word[]="Eggman";
    char input[4096];
    printf("Guess the word:");
    scanf("%s", input);
if (word[0]==input[0])
    {
        printf("E");
    }
else
{
    printf("?");

}
if (word[1]==input[1])
    {
        printf("g");
    }
else
{
    printf("?");

}
if (word[2]==input[2])
    {
        printf("g");
    }
else
{
    printf("?");

}
if (word[3]==input[3])
    {
        printf("m");
    }
else
{
    printf("?");

}
if (word[4]==input[4])
    {
        printf("a");
    }
else 
{
    printf("?");

}
if (word[5]==input[5])
    {
        printf("n");
    }
else
{
    printf("?");

}

}
schaiba
  • 362
  • 3
  • 11
Barry B Benson
  • 331
  • 1
  • 11
  • What is the character stored in e.g. `word[0]`? In `word[1]`? Etc... Now, how do you print a single character with `printf`? Once you have that, and know how to use `for` loops (all of this should be explained in any [good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), so get a couple), you are almost done. The "hard" part is to be able to handle the case when the user inputs *less* than six characters, or no characters at all. – Some programmer dude Dec 10 '17 at 17:22
  • Ok, so should I use nested for loops, or regular ones? – Barry B Benson Dec 10 '17 at 17:23

1 Answers1

0

As you asked there is much easier way to solve your problem instead of definend multiple if else you can use loop.
I have tried to solve your problem, hope it will help:

#include<stdio.h>
int main()
{
   char word[]="Eggman";
    char input[4096];
    char abc[] ={0};
    int i=0,f=0;
    printf("Guess the word:");
    scanf("%s", input);
    while(1){
            if(strcmp(input,word)==0)
            {

                break;
            }
            else
            {
               if(f==1){
                    scanf("%s", input);
                       }
              else
                       {
                    f=1;
                       }
            }
          for(i=0;i<strlen(word);i++)
           {

             if(input[i]==word[i])
               {

                 printf("%c",input[i]);
               }
             else
               {
                printf("?");
               }

           }
         }
       }

The above program will ask your again and again till the user input matched with the declared string.

Lalit Verma
  • 782
  • 10
  • 25