Advertisement

接收一行字符并计算其中英文字母、空格、数字和其他字符的数量

阅读量:

题目:输入一段字符串,分别计算其中英文字母、空格、数字以及其余字符的数量。
1.程序分析:通过while循环实现,判断条件为输入的字符不等于’\n’.
自己编写的代码:

复制代码
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int main()
    {
    	int i;
    	static int zimu,shuzi,kou,qita;
    	char *p=NULL;
    	p=(char *)malloc(12);
    	printf("Please input string:\n");
    	gets(p);
    	int len=strlen(p);
    	printf("len=%d\n",len);
    	for(i=0;i<=len;i++)
    	{
    		if(((*p<='z')&&(*p>='a'))||((*p<='Z')&&(*p>='A')))
    		{
    			zimu++;
    		}
    		else if(*p==' ')
    		{
    			kou++;
    		}
    		else if(*p>='0'&&*p<='9')
    		{

全部评论 (0)

还没有任何评论哟~