编写一个函数stat(...),实现对一行字符分别统计出其中英文字母,空格和其它字符

来源:百度知道 编辑:UC知道 时间:2024/06/13 22:44:55
程序要求如下
(1)一行字符中英文字母的个数由函数stat(...)直接返回;
(2)除编写上述要求的函数外,在编写一个主函数,要求由用户从键盘输入一行字符,主函数通过调用stat(...)函数获得这行字符的英文字母、空格及其它字符的个数,并将它们的只输出。
c 语言

不知道你什么语言。我用JAVA写的。思路就这样了,其他语言自己换
import java.io.*;

public class stat {
public static void main(String args[])
{
String inValue="";
try
{
InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader bufin = new BufferedReader(stdin);
inValue = bufin.readLine();
new stat(inValue);
}catch(IOException E)
{
System.out.println("发生I/O错误!!!");
}
}
public stat(String strIn)
{
int strNum=0;
int spaceNum=0;
int numberNum=0;
int otherNum=0;
strIn=strIn.toUpperCase();
for(int i=0;i<strIn.length();i++)
{
if(strIn.charAt(i)==' ')
{
spaceNum+=1;
}
else if(strIn.charAt(i)>='A' && strIn.charAt(i)<='Z')
{
strNum+=1;
}
else if(st