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("?");
}
}