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?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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 Sample Output

pig

2

goat

toad

goat

 

i got 2 that arent working right sometimes it fails here they are 

 

1st answer 

#include <stdio.h>
#include <string.h>
 
intmain()
{
// input the previous word
charprev_word[20];
scanf("%s",&prev_word);
intn;
scanf("%d",&n);
chararray[n][20];
for(inti=0;i<n;i++)
{
scanf("%s",&array[i]);
}
printf("\n");
// create a result array of strings
charresult[n][20];
intk=0;
for(inti=0;i<n;i++)
{
// take the last character of previous word
chara=prev_word[strlen(prev_word)-1];
// take the first character of the current word
charb=array[i][0];
// if both are equal then add that word to the result array
if(a==b)
{
strcpy(result[k],array[i]);
k++;
}
}
// if the size of result array is zero then print a ?
if(k==0)
{
printf("?");
}
else
{
// otherwise set a flag variable to 0 and iterate the result array
intflag=0;
for(inti=0;i<k;i++)
{
// copy the current word of result array to a temporary word
chartemp[20];
strcpy(temp,result[i]);
// set m = 0
intm=0;
// iterate over the input array
for(intj=0;j<n;j++)
{
// if the current word of input array and temp are same then do nothing
if(strcmp(array[j],temp)==0)
{
// do nothing
}
// otherwise check for first character of current word of input array and last character of temp, if both are same then set m=1
else
{
chara=array[j][0];
charb=temp[strlen(temp)-1];
if(a==b)
{
m=1;
}
}
}
// if m is 0 then print temp with ! and set flag=1 and break from the loop
if(m==0)
{
printf("%s!",temp);
flag=1;
break;
}
}
// if m is 0 then print temp with ! and set flag=1 and break from the loop
if(flag==0)
printf("%s",result[k-1]);
}
return0;
}
 
2nd answer
 
#include <stdio.h>
#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]);
        }
        
    }
}
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education