//编写程序读取输入,读到#停止,报告ei出现的次数
#include
#define STOP '#'
int main(void)
{
char ch, prev;
int count=0;
while ((ch = getchar()) != STOP)
{
if (ch == 'i' && prev == 'e')
count++;
prev = ch;
}
printf("There are %d times 'ei' in the sentense.", count);
return 0;
}
这个代码是在你的文章里看到的,关于里面while循环的部分,prev这样用就可以实现,我不太懂,可以请教一下吗?
这代码是有问题的。prev没有初始值。万一正好分配的空间内的值是'e'呢?那么如果输入的第一个字符是'i',计数就会加1。这可能是错误的。比如输出入字符串为ice呢?