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:
Write a C program that asks the user to guess your favorite Florida city. The user can't exceed 4...
(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 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...