You are playing a game in which a group of players take turns saying animal names. The animal name you say when it is your turn must start with the same letter as the previously said animal ends with and it must not have been said previously in this round of the game. If there is no valid name or you cannot come up with one you are eliminated. Given the last animal name said before your turn and a list of all names not yet used, can you make it through this turn? If so, can you make sure to eliminate the next player?
C programming
You are playing a game in which a group of players take turns saying animal names. The animal name you say when it is your turn must start with the same letter as the previously said animal ends with and it must not have been said previously in this round of the game. If there is no valid name or you cannot come up with one you are eliminated.
Given the last animal name said before your turn and a list of all names not yet used, can you make it through this turn? If so, can you make sure to eliminate the next player?
Input
The first line of input contains a single word, the animal that the previous player just said. The next line contains a single integer nn (0≤n≤1050≤n≤105), the number of valid unused animal names. Each of the following nn lines contains one valid unused animal name.
All animal names (including the one the previous player said) are unique and consist of at least 11 and at most 2020 lower case letters ‘a’-‘z’.
Output
If there is any animal name you can play that eliminates the next player, output the first such name from the input list, followed by an exclamation mark. Otherwise, if there is any animal name that you can play, output the first such name. Otherwise, output a question mark (in this case you will just have to make up a fake name in the hope that the others will trust you that this is a real animal).
Sample Input | Sample Output |
---|---|
pig 2 goat toad |
goat |
i got 2 that arent working right sometimes it fails here they are
1st answer
#include <string.h>
int main()
{
char prev_word[20];
scanf("%s",prev_word);
int n;
scanf("%d",&n);
char array[n][20];
for (int i = 0; i < n; i++)
scanf("%s", array[i]);
// create a result array
char res[n][20];
// initialize k = 0 for res array size
int k=0;
// run a loop n times
for(int i=0;i<n;i++)
{
// check if last character of previous word is equal to
// first character of current word
if(prev_word[strlen(prev_word)-1]==array[i][0])
{
// copy the current word to result array
strcpy(res[k],array[i]);
// increment k
k++;
}
}
if (k==0)
{
printf("?");
}
else
{
// take a flag variable
int f=0;
// run a loop k (size of res) times
for(int i=0;i<k;i++)
{
char word[20];
// copy the current word of res array to word
strcpy(word,res[i]);
// set a variable m to 0
int m=0;
for(int j=0;j<n;j++)
{
// the current word in the input array and the word
// of res array are same then do nothing
if(strcmp(array[j],word)==0)
{
// do nothing
}
else
{
// otherwise check if first character of the word of input
// array is same as last character of the word of res array
if(array[j][0]==word[strlen(word)-1])
// set m to 1
m=1;
}
}
// if m is still 0 then print the word of res array along with '!'
// and set the flag variable to 1
if (m==0)
{
printf("%s!",word);
f=1;
break;
}
}
// if the flag varible is still 0 then print the last word of the res array
if(f==0)
{
printf("%s",res[k-1]);
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 3 images