Question

Write a C program that asks the user to guess your favorite Florida city. The user cant exceed 4 trials. Sample Run 1: Guessplease explain!!! thanks!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

There's no builtin function which compares two strings in c ignoring their cases. So, i've written a custom function to do that for you.

Code:

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
int compare(char *fav, char *ans)//this function returns 0 if two strings are same and 1 otherwise
{
   if(strlen(fav) != strlen(ans)) return 1;//if lengths aren't the same return 1
   int i;
   for(i=0;i<strlen(fav);i++)
   {
       if(toupper(fav[i]) != toupper(ans[i])) return 1;//for every character convert it into upper case and check if same, if they aren't same, return 1
   }
   return 0;//finished scanning the strings, no non-matching character appeared so return 0
}
int main()
{
   char fav[] = "Kissimmee";//stire your favourite city here
   int trial = 0;//count no of trials
   char *ans;ans = (char *)malloc(100*sizeof(char));//allocate memory for user input
   printf("Guess my favourite Florida city: ");
   fgets(ans, 99, stdin);//reads a line of input including spaces
   ans[strlen(ans)-1]='\0';//to avoid storing the newline character
   while(1)//run infinitely
   {
       trial++;//increment trial
       if(compare(fav,ans)==0)//if both are same
       {
           printf("That's the one!\n");
           break;
       }
       if(trial<4)//if trials haven't exceeded 4
       {
           printf("No! Try again: ");
           fgets(ans, 99, stdin);
           ans[strlen(ans)-1]='\0';
       }
       else//trials are more than 4
       {
           printf("Sorry-my favorite Florida City is %s!",fav);
           break;
       }
   }
   return 0;
}

Output:

Guess my favourite Florida city: Orlando No! Try again: MiAmi No ! Try again: Fort meyers Thats the one! Process exited afte

Guess my favourite Florida city: Orlando No! Try again: Tampa No! Try again: Fort meyers No! Try again: Sarasota Sorry-my fav

Add a comment
Know the answer?
Add Answer to:
Write a C program that asks the user to guess your favorite Florida city. The user can't exceed 4...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (c++) Write a program that generates a random number between 1 and 100 and asks the...

    (c++) Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Be sure that your program...

  • Write a C program that asks the user to enter three numbers (integres). A menu will...

    Write a C program that asks the user to enter three numbers (integres). A menu will be displayed to let the user choose one of the three options: print the two highest numbers, product of the three numbers or the division of the second by the by the third if the third is not zero. See the sample runs. Required: Your code must use -One function to get the numbers from the user One function for each of the three...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT