C语言指针作业中出现的自动关闭的错误

来源:百度知道 编辑:UC知道 时间:2024/05/04 06:01:20
题目:输入一行文字找出其中大写字母,小写字母,空格,数字,以及其他字符各有多少。

#include <stdio.h>
#include <conio.h>
void main()
{
int upper=0,lower=0,digit=0,space=0,other=0,i=0;
char *p,s[20];
printf("Please input string:");
while ((s[i]=getchar())<='\n')
i++;
p=&s[0];
while (*p!='\n')
{
if (('A'<=*p)&&(*p<='Z'))
++upper;
else if (('a'<=*p)&&(*p<='z'))
++lower;
else if (*p=' ')
++space;
else if ((*p<='9')&&(*p>='0'))
++digit;
else
++other;
p++;
}
printf ("upper case:%d lower case:%d",upper,lower);
printf ("space:%d digit:%d other:%d\n",space,digit,other);
getch();
}

这个程序可以运行,但是输入完毕之后会出现数字一样的错误。
然后TC3会出错误自动关闭

我用的编译器是 Turbo C 3.0

//分给拉
#include <stdio.h>
#include <conio.h>
void main()
{
int upper=0,lower=0,digit=0,space=0,other=0,i=0;
char *p,s[20];
printf("Please input string:");
while ((s[i]=getchar())!='\n') //这也错啦!!
i++;
p=s;
while (*p!='\n')
{
if (('A'<=*p)&&(*p<='Z'))
++upper;
else if (('a'<=*p)&&(*p<='z'))
++lower;
else if (*p==' ') //少写了一个等号
++space;
else if ((*p<='9')&&(*p>='0'))
++digit;
else
++other;
p++;
}
printf ("upper case:%d lower case:%d",upper,lower);
printf ("space:%d digit:%d other:%d\n",space,digit,other);
//getch();这个就不需要的吧
}

while ((s[i]=getchar())!='\n')这里要改一下。

while ((s[i]=getchar())<='\n')
改成
while ((s[i]=getchar())!='\n