求解决统计字母个数小问题

Johnson310103 2008-04-01 03:17:00
#include<iostream>
#include<cstdio>
using namespace std;
int n;
void show(char *a)
{
int i,j=0;
for(i=0;a;i++)
{
if((a[i]>'a'&&a[i]<'z')||(a[i]>'A'&&a[i]<'Z'))
j++;
}
cout<<"共有"<<j<<"个字母!"<<endl;
}

void main()
{
int i;
char b[100];
cout<<"input the sentence:"<<endl;
for(i=0;i<100;i++)
cin>>b[i];
show(b);
}
怎么就出来不了结果呢?
...全文
108 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
gl0312 2008-04-01
  • 打赏
  • 举报
回复
支持六楼
Johnson310103 2008-04-01
  • 打赏
  • 举报
回复
就是输入句子过后就运行不出结果来
liveforme 2008-04-01
  • 打赏
  • 举报
回复
奥?我是根据lz意思来的,随便改了下,你那里报什么错了?
Johnson310103 2008-04-01
  • 打赏
  • 举报
回复
回liveforme,你的那个复杂了点吧;Chappell的运行了可显示不对呀
Chappell 2008-04-01
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
void show(const char *a)
{
int i,j=0;
for(i=0;a[i]!='\0';i++)
{
if((a[i]>'a'&&a[i] <'z') ||(a[i]>'A'&&a[i] <'Z'))
j++;
}
cout <<"共有" <<j <<"个字母!" <<endl;
}

void main()
{
string str;
cout <<"input the sentence:" <<endl;
cin>>str;
show(str.c_str());
}
liveforme 2008-04-01
  • 打赏
  • 举报
回复
我想你定义全局的n 是这个意思吧
liveforme 2008-04-01
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>

#define pause system("pause")

using std::cin;
using std::cout;
using std::endl;
using std::string;

#include<iostream>

int n;
void show(char *a)
{
int i,j=0;
for(i=0;i<n;i++)
{
if((a[i]>='a'&&a[i] <='z') || (a[i]>='A'&&a[i] <='Z'))
j++;
}
cout <<"共有" <<j <<"个字母!" <<endl;
}

void main()
{
int i = 0;
char b[100];
cout <<"input the sentence:" <<endl;
for(i = 0; i<100; i++)
b[i] = NULL;
i = 0;
while((b[i]=getchar())!='\n'){
i++;
}
n = i;
show(b);
pause;
}
bargio_susie 2008-04-01
  • 打赏
  • 举报
回复
for(i=0;i <100;i++)
cin>>b[i];

_______________________________________
!!!!这是啥??直接 cin >> b 不就得了。
Supper_Jerry 2008-04-01
  • 打赏
  • 举报
回复
for(i=0;i<100;i++)
bargio_susie 2008-04-01
  • 打赏
  • 举报
回复
首先这里就不对

for(i=0;a;i++)
{
if((a[i]>'a'&&a[i] <'z') ¦ ¦(a[i]>'A'&&a[i] <'Z'))
j++;
}
——————————————————————————————
for(i=0; a[i]!= '\0'; i++)
{
if((a[i]>='a'&&a [i] <= 'z') ¦ ¦(a[i]>='A'&&a[i] <='Z'))
j++;
}

64,849

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧