
用C++编写一个函数,统计一个英文句子中字母的个数,在主程序中实现输入输出,必须是C++啊
2个回答
展开全部
我这有个和你类似的源代码,统计数字、符号、其他的个数,要求以“|”为开始 结束符,做法和你的一样,看一下吧
#include <iostream>
#include <string.h>
//#include <stdio.h>
using namespace std;
int main()
{
char a[1000];
int num=0;
int fuhao=0;
int bb=0;
cin>>a;
int y=0;
for(y=0;y<(strlen(a));y++)
{
switch(a[y])
{
case '|':
{
y++;
goto label;
}
}
}
label:
for(int i=y;i<(strlen(a));i++)
{
switch(a[i])
{
case '1':
{
num++;
break;
}
case '2':
{
num++;
break;
}
case '3':
{
num++;
break;
}
case '4':
{
num++;
break;
}
case '5':
{
num++;
break;
}
case '6':
{
num++;
break;
}
case '7':
{
num++;
break;
}
case '8':
{
num++;
break;
}
case '9':
{
num++;
break;
}
case '0':
{
num++;
break;
}
case '+':
{
fuhao++;
break;
}
case '-':
{
fuhao++;
break;
}
case '*':
{
fuhao++;
break;
}
case '/':
{
fuhao++;
break;
}
case '=':
{
fuhao++;
break;
}
case '%':
{
fuhao++;
break;
}
case '|':
{
goto d;
}
default:
{
bb++;
break;
}
}}
d:
{
cout<<num<<" "<<fuhao<<" "<<bb<<endl;
}
return 0;
}
#include <iostream>
#include <string.h>
//#include <stdio.h>
using namespace std;
int main()
{
char a[1000];
int num=0;
int fuhao=0;
int bb=0;
cin>>a;
int y=0;
for(y=0;y<(strlen(a));y++)
{
switch(a[y])
{
case '|':
{
y++;
goto label;
}
}
}
label:
for(int i=y;i<(strlen(a));i++)
{
switch(a[i])
{
case '1':
{
num++;
break;
}
case '2':
{
num++;
break;
}
case '3':
{
num++;
break;
}
case '4':
{
num++;
break;
}
case '5':
{
num++;
break;
}
case '6':
{
num++;
break;
}
case '7':
{
num++;
break;
}
case '8':
{
num++;
break;
}
case '9':
{
num++;
break;
}
case '0':
{
num++;
break;
}
case '+':
{
fuhao++;
break;
}
case '-':
{
fuhao++;
break;
}
case '*':
{
fuhao++;
break;
}
case '/':
{
fuhao++;
break;
}
case '=':
{
fuhao++;
break;
}
case '%':
{
fuhao++;
break;
}
case '|':
{
goto d;
}
default:
{
bb++;
break;
}
}}
d:
{
cout<<num<<" "<<fuhao<<" "<<bb<<endl;
}
return 0;
}
展开全部
#include<iostream>
#include <string>
using namespace std ;
int main()
{
string Str;
char ch ;
int i=0,cnt=0;
cout << "input str:" ;
getline(cin , Str );
for ( i=0;i<Str.length();i++ )
{
if ( Str[i] >='a' && Str[i]<='z' || Str[i] >='A' && Str[i]<='Z' )
cnt++;
}
cout << "str="<<Str<<endl ;
cout << "字母个数:" << cnt <<endl ;
system("pause");
return 0;
}
#include <string>
using namespace std ;
int main()
{
string Str;
char ch ;
int i=0,cnt=0;
cout << "input str:" ;
getline(cin , Str );
for ( i=0;i<Str.length();i++ )
{
if ( Str[i] >='a' && Str[i]<='z' || Str[i] >='A' && Str[i]<='Z' )
cnt++;
}
cout << "str="<<Str<<endl ;
cout << "字母个数:" << cnt <<endl ;
system("pause");
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询