今天也是男朋友写的!!!
7-11 统计数字字符和空格 (10 分)
本题要求编写程序,输入一行字符,统计其中数字字符、空格和其他字符的个数。建议使用switch语句编写。
输入格式:
输入在一行中给出若干字符,最后一个回车表示输入结束,不算在内。
输出格式:
在一行内按照
blank = 空格个数, digit = 数字字符个数, other = 其他字符个数
的格式输出。请注意,等号的左右各有一个空格,逗号后有一个空格。
输入样例:
在这里给出一组输入。例如:
Reold 12 or 45T
输出样例:
在这里给出相应的输出。例如:
blank = 3, digit = 4, other = 8
#include <iostream>
using namespace std;
#include<string>
void ccc(char abc)
{
int a=0,b=0,c=0;
while((abc=cin.get())!='\n')
{
if(abc>='0'&&abc<='9')
{
a++;
}
else if(abc==' ')
{
b++;
}
else
c++;
}
cout<<"blank = "<<b<<","<<' '<<"digit = "<<a<<","<<' '<<"other = "<<c <<endl;
}
int main ()
{
char abc;
ccc(abc);
return 0;
}
还有一个也是男朋友沿着我的思路写的~
#include <iostream>
using namespace std;
#include<string>
void ccc(char abc[],int n)
{
int a=0,b=0,c=0;
int i;
for (i=0;i<n;i++)
{
if(abc[i]>='0'&&abc[i]<='9')
{
a++;
}
else if(abc[i]==' ')
{
b++;
}
else
{
c++;
}
}
cout<<"blank = "<<b<<","<<" digit = "<<a<<","<<" other = "<<c<<endl;
}
int main()
{
char abc[1000];
char ch;
int i;
int n=0;
ch=getchar();
for(i=0;ch!='\n';i++)
{
abc[i]=ch;
n++;
ch=getchar();
}
ccc(abc,n);
return 0 ;
}
数组写法,思路差不多~
再次感谢男朋友!!!!