题目:
判断两个字符串是否相等
源代码:
#include <stdio.h>
#include <string.h>
#define ANSWER "Grant"
#define SIZE 40
char * s_gets(char * st,int n);
int main(){
char try[SIZE];
puts("Who is buried in Grant's tomb?");
s_gets(try,SIZE);
while(strcmp(try,ANSWER) != 0){
puts("No,that's wrong.Try again.");
s_gets(try,SIZE);
}
puts("Thar's right!");
return 0;
}
char * s_gets(char * st,int n){
char * ret_val;
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;
}
return ret_val;
}
演示效果:
代码优化:
无论用户大写还是小写,该程序都能识别
源代码:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define ANSWER "GRANT"
#define SIZE 40
char * s_gets(char * st,int n);
void TOUPPER(char * st);
int main(){
char try[SIZE];
puts("Who is buried in Grant's tomb?");
s_gets(try,SIZE);
TOUPPER(try);
while(strcmp(try,ANSWER) != 0){
puts("No,that's wrong.Try again.");
s_gets(try,SIZE);
TOUPPER(try);
}
puts("Thar's right!");
return 0;
}
char * s_gets(char * st,int n){
char * ret_val;
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;
}
return ret_val;
}
void TOUPPER(char * st){
while(*st != '\0'){
*st = toupper(*st);
st++;
}
}
演示效果:
如果朋友你感觉文章的内容对你有帮助,可以点赞,关注文章和专栏以及关注我哈,嘿嘿嘿我会定期更新文章的,谢谢朋友你的支持哈