首页 > 试题广场 >

用ctype.h中的函数重写下面的程序,使得不管用户选择的是

[问答题]
用ctype.h中的函数重写下面的程序,使得不管用户选择的是大写还是小写,程序都可以识别正确答案。
/* compare.c -- 这个程序可以满足要求 */
#include <stdio.h>
#include <string.h>   /* 声明strcmp( )函数 */
#define ANSWER "Grant"
#define MAX 40
int main (void)
{
char try[MAX];
puts ("Who is buried in Grant's tomb?");
gets (try);
while (strcmp (try, ANSWER) != 0)
{
puts (No, that's wrong. Try again.");
gets (try);
}
puts ("That's right!");
return 0;
}

推荐
下面是一种方案:
/* compare.c -- 可行方案 */
#include <stdio.h>
#include <string.h>  /* 声明strcmp( )函数 */
#include <ctype.h>
#define ANSWER "GRANT"
#define MAX 40
void ToUpper (char * str);
int main (void)
{
char try[MAX];
puts ("Who is buried in Grant's tomb?");
gets (try);
YoUpper (try);
while (strcmp (try, ANSWER) != 0
{
puts ("No, that's wrong. Try again.");
gets (try);
ToUpper (try);
}
puts (That's right!");
return 0;
}
void ToUpper (char * str)
{
whle (*str != '\0')
{
*str = toupper (*str);
str++;
}
}

发表于 2018-03-23 21:45:26 回复(0)