69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <ctype.h>
#define STOP '#'
#define MARK 'e'
#define MARK_NEXT 'i'
int main(void){
char cur;
char cur_low;
char prev;
int count=0;
while((cur=getchar())!=STOP){
cur_low = tolower(cur);
if(cur_low==MARK&&((prev=getchar())==MARK_NEXT)){
count++;
}
}
if(count==1||count==0)
printf("'ei' has appearanced 1 time.");
else
{
printf("'ei' has appearanced %d times.",count);
}
return 0;
}
if(count==1||count==0) //这是你写的,count无论是0和1都是输出1啊
printf("'ei' has appearanced 1 time.");
int main(void){
int cur;//为了判断EOF,cur应该是整型
char cur_low;
char prev =0;
char count=0;
cur = getchar();//多写一个ch=getchar()并不复杂,没必要把它放在循环条件中
while ((cur != EOF) && (cur != STOP)){//对输入应该增加EOF的判断
cur_low = tolower(cur);
if ((prev == MARK) && (cur_low == MARK_NEXT)){
//第一次时prev是0,此后prev是上一次的cur_low,不要再去读取。
count++;
}
prev = cur_low;
cur = getchar();//多写一个ch=getchar()并不复杂,没必要把它放在循环条件中
}
if(count==1||count==0)
printf("'ei' has appearanced %d time.", count);
else
{
printf("'ei' has appearanced %d times.",count);
}
return 0;
}
#include <stdio.h>
#include <ctype.h>
#define STOP '#'
#define MARK 'e'
#define MARK_NEXT 'i'
int main(void){
char cur;
char cur_low;
char next;
int count=0;
while((cur=getchar())!=STOP){
cur_low = tolower(cur);
if(cur_low==MARK&&((next=getchar())==MARK_NEXT)){
count++;
}
}
if (count==0||count==1)
printf("'ei' has appearanced %d time.",count);
else
printf("'ei' has appearanced %d times.",count);
return 0;
}
至于while((cur=getchar())!=STOP)这句话是在C Primer Plus上看来的。。说是比较精简的表达方式。
最后。再次感谢各位大大的指导。特别是4L。。每一句都加了注释。好认真。