| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 12387 人关注过本帖
标题:在主函数中输入一个字符串str,调用函数统计字符串中出现的字母,(含大小写 ...
只看楼主 加入收藏
王文倩
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2013-4-23
结帖率:0
收藏
已结贴  问题点数:20 回复次数:8 
在主函数中输入一个字符串str,调用函数统计字符串中出现的字母,(含大小写)、数字、空格以及其他字符出现的次数,在主函数中输出统计结果。
#include<iostream>
#include<string>
using namespace std;
void search(char *s,char c[]);
int main()
{
    int a,b,f,d;
    char string[100];
    cout<<"please enter string:"<<endl;
    cin>>string;
    char *p;
    p=string;
    search(p,string);
    cout<<"a="<<a<<endl;
    cout<<"b="<<b<<endl;
    cout<<"f="<<f<<endl;
    cout<<"d="<<d<<endl;
    cout<<endl;
   return 0;
}
void search(char *s,char c[])
{
    int i;
    int a=0,b=0,f=0,d=0;
    int stren=0;
    s=c;
    while(*s!='\0')
    {
        s++;
        stren++;
    }
    for(i=0;i<=stren;i++)
    {
        if((c[i]>'a'&&c[i]<'z')||(c[i]>'A'&&c[i]<'Z'))
        {
            a++;
        }
        else if(c[i]>0&&c[i]<9)
        {
            b++;
        }
        else if(c[i]==' ')
        {
            f++;
        }
        else
        {
            d++;
        }
    }
}
有哪位高手等帮我看一下,c++初学,不知道哪个地方错了,输出结果就是不对
搜索更多相关主题的帖子: include search please 
2013-04-23 10:29
xufan
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:232
专家分:804
注 册:2008-10-20
收藏
得分:5 
给你看了下呢,修改了如下代码
程序代码:
#include<iostream>
#include<string>
using namespace std;
int a=0,b=0,f=0,d=0;
void search(char *s,char c[]);
int main()
{
    //int a,b,f,d;
    char string[100];
    cout<<"please enter string:"<<endl;
    cin>>string;
    char *p;
    p=string;
    search(p,string);
    cout<<"a="<<a<<endl;
    cout<<"b="<<b<<endl;
    cout<<"f="<<f<<endl;
    cout<<"d="<<d<<endl;
    cout<<endl;
   return 0;
}
void search(char *s,char c[])
{
    int i;
    //int a=0,b=0,f=0,d=0;
    int stren=0;
    s=c;
    while(*s!='\0')
    {
        s++;
        stren++;
    }
    for(i=0;i<=stren;i++)
    {
        if((c[i]>'a'&&c[i]<'z')||(c[i]>'A'&&c[i]<'Z'))
        {
            a++;
        }
        else if(c[i]>'0'&&c[i]<'9')
        {
            b++;
        }
        else if(c[i]==' ')
        {
            f++;
        }
        else
        {
            d++;
        }
    }
}

说明一下:
1.主函数的变量a,b,c,d与search里面的a,b,c,d没有任何关系
2.string是一个关键字,不建议作为变量名.
3.string[0]是一个字符,而不是一个整型

~~~~~~我的明天我知道~~~。
2013-04-23 13:57
zhuxiaoneng
Rank: 4
等 级:业余侠客
威 望:2
帖 子:51
专家分:215
注 册:2013-4-10
收藏
得分:5 
程序代码:
#include<iostream>
#include<string>
using namespace std;
void search(char *s,char c[]);
int main()
{
    //int a,b,f,d;                            //这个地方不需要,因为这些值是在search函数里面进行统计的,你没有把这些值传进去,也没有传出来
    char string[100];                         //建议看看传值与传址 
    cout<<"please enter string:"<<endl;
    //cin>>string;
    gets(string); //cin >> string这个会在遇到空格的时候,被截取,比如"hello world" string 中只会有"hello"
    char *p;
    p=string;
    search(p,string);
    //cout<<"a="<<a<<endl; 都不需要,在search函数中进行统计
    //cout<<"b="<<b<<endl;
    // cout<<"f="<<f<<endl;
    // cout<<"d="<<d<<endl;
    // cout<<endl;
    return 0;
}
void search(char *s,char c[])
{
    int i;
    int a=0,b=0,f=0,d=0;
    int stren=0;
    s=c;
    while(*s!='\0')
    {
        s++;
        stren++;
    }
    for(i=0;i < stren;i++)
    {
        //if((c[i]>'a'&&c[i]<'z')||(c[i]>'A'&&c[i]<'Z')) 应该为
        if((c[i]>='a'&&c[i]<='z')||(c[i]>='A'&&c[i]<='Z'))
        {
            a++;
        }
        else if(c[i]>='0' && c[i]<= '9')
        {
            b++;
        }
        else if(c[i]==' ')
        {
            f++;
        }
        else
        {
            d++;
            cout << c[i] << endl;
        }
    }

    cout<<"a="<<a<<endl;
    cout<<"b="<<b<<endl;
    cout<<"f="<<f<<endl;
    cout<<"d="<<d<<endl;
    cout<<endl;
}


建议:C++的话,字符串用string,比较方便
2013-04-23 14:17
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:5 
3楼的同学没看题目要求么?

My life is brilliant
2013-04-23 14:26
zhuxiaoneng
Rank: 4
等 级:业余侠客
威 望:2
帖 子:51
专家分:215
注 册:2013-4-10
收藏
得分:0 
回复 4楼 lz1091914999
确实没有看题目要求
这样返回参数比较多的话,还是搞个结构体封装下,然后传入一个引用或者返回一个该结构体的对象
2013-04-23 15:12
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:5 
#include<iostream>
#include<string>
using namespace std;
void search(char *s,char c[]);
int main()
{
    char string[100];
    cout<<"please enter string:"<<endl;
    cin>>string;
    char *p;
    p=string;
    search(p,string);
     return 0;
}
void search(char *s,char c[])
{
    int i;
    int a=0,b=0,f=0,d=0;
    int stren=0;
    s=c;
    while(*s!='\0')
    {
        s++;
        stren++;
    }
    for(i=0;i<stren;i++)
    {
        if((c[i]>='a'&&c[i]<='z')||(c[i]>='A'&&c[i]<='Z'))
        {
            a++;
        }
        else if(c[i]>=0&&c[i]<=9)
        {
            b++;
        }
        else if(c[i]==' ')
        {
            f++;
        }
        else
        {
            d++;
        }
    }
     cout<<"字母的个数:"<<a<<endl;
    cout<<"数字的个数:"<<b<<endl;
    cout<<"空格的个数:"<<f<<endl;
    cout<<"其它的个数:"<<d<<endl;
    cout<<endl;
}
这样修改就可以了,你的错误原因就是因为,你所声明的变量没有初始化,虽然你在search函数中进行相同名称变量的赋值,但是那些是局部变量,不能被主函数所用的,所以主函数那些变脸就很疯狂了,你输出自然是错误的

Maybe
2013-04-24 00:21
王文倩
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2013-4-23
收藏
得分:0 
回复 2楼 xufan
谢谢了啊!
2013-05-14 11:01
王文倩
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2013-4-23
收藏
得分:0 
回复 2楼 xufan
谢谢了啊!你学的太好了,好羡慕啊!
2013-05-14 11:01
王文倩
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2013-4-23
收藏
得分:0 
回复 3楼 zhuxiaoneng
谢谢了啊!!
2013-05-14 11:02
快速回复:在主函数中输入一个字符串str,调用函数统计字符串中出现的字母,(含大 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018530 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved