编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输入字符串以及输出上述的结果。
#include<stdio.h>
int Letter=0,Digit=0,Space=0,Other=0;
void main()
{
void calculate(char array[]);
char a[40];
printf("input a string:/n");
gets(a);
calculate(a);
printf("letter:%d/ndigit:%d/nspace:%d/nother:%d/n",Letter,Digit,Space,Other);
getch();
}
void calculate(char array[])
{
int i;
for(i=0;array[i]!='/0';i++)
if(array[i]>='a'&&array[i]<='z'||array[i]>='A'&&array[i]<='Z') Letter++;
else if(array[i]>='0'&&array[i]<='9') Digit++;
else if(array[i]==' ') Space++;
else Other++;
}
输入My address is #123 Shanghai Road,Beijing,100045.
输出letter:30
digit:9
space:5
other:4
返回,再按alt+F5,发现下边多了
Divide error
把全局变量初始化去掉:
#include<stdio.h>
int Letter,Digit,Space,Other;
void main()
{
void calculate(char array[]);
char a[40];
printf("input a string:/n");
gets(a);
calculate(a);
printf("letter:%d/ndigit:%d/nspace:%d/nother:%d/n",Letter,Digit,Space,Other);
getch();
}
void calculate(char array[])
{
int i;
for(i=0;array[i]!='/0';i++)
if(array[i]>='a'&&array[i]<='z'||array[i]>='A'&&array[i]<='Z') Letter++;
else if(array[i]>='0'&&array[i]<='9') Digit++;
else if(array[i]==' ') Space++;
else Other++;
}
输入:My address is #123 Shanghai Road,Beijing,100045.
输出:letter:30
digit:9
space:5
other:4
然后弹出对话框:
C:/TC20/TC.EXE
The NTVDM CPU has encountered an illegal instruction.
CS:00a7 IP:03b3 OP:ff ff ff ff 00 Choose 'Close' to terminate the
application.
重新运行保存的程序,在输出结果后返回编辑界面再按Alt+F5,看到下边多出一行:Abnomal program termination.