发布网友
共5个回答
热心网友
不要用scanf输入,用gets,输入字符串的时候,scanf会把空格当成字符串之间的分隔符,如果你输入how
are
you,那么系统把空格当成分隔符,会先把how送入字符串中,把how当成一个字符串处理,所以自动在how后面加'\0',how后面的字符全读不进来.用gets就可以解决这个问题.
#include
"stdio.h"
#include"string.h"
main()
{char
ch[100];
int
i,n,upp=0,low=0,dig=0,spa=0,oth=0;
printf("input
character;\n");
gets(ch);
printf("%s\n",ch);
n=strlen(ch);
for(i=0;i
='a'&&ch[i]<='z')low++;
else
if(ch[i]>='A'&&ch[i]<='Z')upp++;
else
if(ch[i]>'0'&&ch[i]<='9')dig++;
else
if(ch[i]=='
')spa++;
else
oth++;
}
printf("\nupper
case:%d\n",upp);
printf("lower
case
:%d\n",low);
printf("digit
:%d\n",dig);
printf("space
:%d\n",spa);
printf("other
:%d\n",oth);
}
热心网友
不要用scanf输入,用gets,输入字符串的时候,scanf会把空格当成字符串之间的分隔符,如果你输入how are you,那么系统把空格当成分隔符,会先把how送入字符串中,把how当成一个字符串处理,所以自动在how后面加'\0',how后面的字符全读不进来.用gets就可以解决这个问题.
#include "stdio.h"
#include"string.h"
main()
{char ch[100];
int i,n,upp=0,low=0,dig=0,spa=0,oth=0;
printf("input character;\n");
gets(ch);
printf("%s\n",ch);
n=strlen(ch);
for(i=0;i<n;i++)
{if(ch[i]>='a'&&ch[i]<='z')low++;
else if(ch[i]>='A'&&ch[i]<='Z')upp++;
else if(ch[i]>'0'&&ch[i]<='9')dig++;
else if(ch[i]==' ')spa++;
else oth++;
}
printf("\nupper case:%d\n",upp);
printf("lower case :%d\n",low);
printf("digit :%d\n",dig);
printf("space :%d\n",spa);
printf("other :%d\n",oth);
}
热心网友
你读取输入是用scanf函数的吧
你用gets函数读取试试。
以下是一个示意程序:
#include
"stdio.h"
#include
"ctype.h"
#include
"string.h"
int
main(void)
{
char
s[256];
int
i;
int
blank_c=0;
gets(s);
for(i=0;i
评论
0
0
加载更多
热心网友
你读取输入是用scanf函数的吧
你用gets函数读取试试。
以下是一个示意程序:
#include "stdio.h"
#include "ctype.h"
#include "string.h"
int main(void)
{
char s[256];
int i;
int blank_c=0;
gets(s);
for(i=0;i<strlen(s);i++)
{
if(s[i]==' ')
blank_c++;
}
printf("Counts of blanks: %d",blank_c);
return 0;
}
运行结果:
ni hao a
Counts of blanks: 2
热心网友
123