#include
#include
#include
#define ANSWER "Grant"
#define SIZE 40
char *s_gets(char * st, int n);
int main (void)
{
char try1[SIZE];
char answer[] = ANSWER;
int i = 0;
while(answer[i])
{
answer[i] = tolower(answer[i]);
i++;
}
puts("Who is buried in Grant's tomb?");
s_gets(try1, SIZE);
while(strcmp(try1, answer) !=0)
{
puts("No, that's wrong. Try again.");
s_gets(try1, SIZE);
}
puts("That's Right!");
return 0;
}
char * s_gets(char * st, int n)
{
char * ret_val;
char * pc;
int j = 0;
ret_val = fgets(st, n, stdin);
if(ret_val)
{
while((*st) != '\n' && (*st) != '\0')
{
st++;
}
if ((*st) == '\n')
{
(*st) = '\0';
}
else
while(getchar() != '\n')
continue;
}
while(st[j])
{
st[j] = tolower(st[j]);
j++;
}
return ret_val;
}
以下程序可以正常运行
char * ret_val;
int i = 0, j = 0;
ret_val = fgets(st, n, stdin);
if(ret_val)
{
while(st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else
while(getchar() != '\n')
continue;
}
while(st[j])
{
st[j] = tolower(st[j]);
j++;
}
return ret_val;
}*/
char * s_gets(char * st, int n)
{
char * ret_val;
char * trans;
int j = 0;
ret_val = fgets(st, n, stdin);
//将ret_val的首地址赋给trans,保证后面只是trans的地址改变,ret_val的地址不变
trans = ret_val;
if (trans)
{
while ((*trans) != '\n' && (*trans) != '\0')
{
//新增
if (*trans >= 'A' && *trans <= 'Z')
{
*trans = *trans + 32;
printf(ret_val);
}
//该循环结束,位置已经将trans指向了输入字符串的最后+1的地址,内容将为NULL
//st++;
trans++;
}
return ret_val;
}
}
tolower函数没有定义啊
answer[i] = tolower(answer[i]);
这里answer是常量,不能修改