| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1033 人关注过本帖
标题:初学者求助解答几个C++编程
只看楼主 加入收藏
迷途小羔羊
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-11-5
收藏
 问题点数:0 回复次数:4 
初学者求助解答几个C++编程
1. (Occurences of a specified character) Write a function that finds the number of occurrences of a specified character in the string using the following header:
    Int count(const char * const str, char a)
For example, count(“Welcome”, ‘e’) returns 2.
prompt the user to enter a string and a letter


2. (Hex to decimal) Writer a function that parses a hex number as a string into a decimal integer. The function header is as follows:
    Int parseHex(const char * const hexString)
For example, hexString A5 is 165 (10x16+5=165) and FAA is 4100 (15x16^2+10x16+10=4100). So, parseHex (“A5”) returns 165 and parseHex(“FAA”) returns 4100. Use hex strings ABC and 10A to test the function.
prompt the user to enter a hexadecimal number


3. (Sorting characters in a string) Write two overloaded functions that return a sorted string using the following header:
    Char * sort(char *s)
    Void sort(const char * const s, char * s1)

For example, sort(“acb”) return abc.
you will have to allocate memory for the sorted array
搜索更多相关主题的帖子: 解答 
2008-11-05 00:14
taiyang0331
Rank: 2
等 级:论坛游民
帖 子:35
专家分:20
注 册:2008-9-24
收藏
得分:0 
NO.1
int count(const char * const str, char a)
{
    char const *p = str;
    int i = 1;

    while (*p++ != a)
    {
        i += 1;

        if(*p == '\0')
            return 0;
    }

    return i;
}
2008-11-05 10:54
taiyang0331
Rank: 2
等 级:论坛游民
帖 子:35
专家分:20
注 册:2008-9-24
收藏
得分:0 
NO.2
int parseHex(const char * const hexString)
{
    const char *dest = hexString;
    int value = 0;

    while('\0' != *dest)
    {
        switch(*dest)
        {
        case 'A':
            value += 10;
            break;
        case 'B':
            value += 11;
            break;
        case 'C':
            value += 12;
            break;
        case 'D':
            value += 13;
            break;
        case 'E':
            value += 14;
            break;
        case 'F':
            value += 15;
            break;
        default:
            value += atoi(dest);
            break;
        }        
        value *= 16;
    
        dest++;
    }

    return value / 16;
}
有点麻烦, 先想到这的  也是新手
2008-11-05 11:35
迷途小羔羊
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-11-5
收藏
得分:0 
看了这个终于有点头绪了````谢谢阿!!!!
2008-11-05 23:16
zxf12604
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-11-3
收藏
得分:0 
#include <iostream>
using namespace std;
int fun(const char *const ,char );
void main()
{
    cout<<"输入一个单词:"<<'\n';
    char a[10];
    cin.getline(a, 10);
    
    const char *const str=&a[0];
    cout<<"输入要查的字母:"<<'\n';
    char p;
    cin>>p;
    int x=fun (str,p);
    cout<<x<<endl;


}
int fun (const char *const str,char n)
{   
    const char *z=str;
          int j=1;
    while (*z!=n)
    {
        j++;
        if(*z=='\0')
            return 0;
        z++;

    }
    return j;
    
}
2008-11-06 10:44
快速回复:初学者求助解答几个C++编程
数据加载中...
 
   



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

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