治真菌感染的药:计算机编程问题

来源:百度文库 编辑:高校问答 时间:2024/04/27 18:34:31
编写程序,输入一字符串,统计其中数字和英文字母出现次数,并输出

嗯,是啊,不同语言写出来的程序不一样,不过算法类似,下面是我用C语言写的,你可以参看一下。
void main()
{
int i = 0,countNum=0,countChar=0;
char str[50];
printf("please input a sentence: ");
scanf("%s",&str);
while(str[i]!='\0')
{
if(str[i]>='0' && str[i]<='9')
countNum=countNum+1;
else if((str[i]>='a'&&str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
countChar=countChar+1;
i=i+1;
}
printf("There are %d numbers and %d chars in this sentence!\n",countNum,countChar);
}

你也不说用什么语言?怎么说啊?


char str[100];
scanf("%s",str);来输入

int charcount,numcount;
char p = str[0];
while (p != '\0')
{
if ((p >= 'a' and p <= 'z') or (p >= 'A' and p <= 'Z') )
charcount++;
else if (p>=48 and p<=58 )
numcount++;
p=++str;
}