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 1 Sample Output 1 pig 2 goat toad goat   Sample Input 2 Sample Output 2 dog 2 snake emu ?   Sample Input 3 Sample Output 3 hare 3 bee cat eagle eagle!   mine isnt working right sometimes it fails here is mine    #include #include // main function intmain() { // input previous word charprev_word[20]; scanf("%s",prev_word); // input n intn; scanf("%d",&n); // input array of n strings chararray[n][20]; for(inti=0;i

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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 1 Sample Output 1

pig

2

goat

toad

goat

 

Sample Input 2 Sample Output 2

dog

2

snake

emu

?

 

Sample Input 3 Sample Output 3

hare

3

bee

cat

eagle

eagle!

 

mine isnt working right sometimes it fails here is mine 

 

#include <stdio.h>
#include <string.h>
// main function
intmain()
{
// input previous word
charprev_word[20];
scanf("%s",prev_word);
// input n
intn;
scanf("%d",&n);
// input array of n strings
chararray[n][20];
for(inti=0;i<n;i++)
scanf("%s",array[i]);
// create a result array
charres[n][20];
// initialize k = 0 for res array size
intk=0;
// run a loop n times
for(inti=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 the size of res array is 0 then print ?
if(k==0)
{
printf("?");
}
else
{
// take a flag variable
intf=0;
// run a loop k (size of res) times
for(inti=0;i<k;i++)
{
// declare a word
charword[20];
// copy the current word of res array to word
strcpy(word,res[i]);
// set a variable m to 0
intm=0;
// now run a loop n times
for(intj=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]);
}
 
}
}
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 6 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY