字符串转换c ++

时间:2012-07-26 14:12:45

标签: c++ char

我有一个字符串,第一个元素是例如'a'。我已经声明了一个名为a的变量(例如int int = 1)。我现在的问题是,如何将整个字符串转换为数字(a = 1,b = 2,c = 3,... z = 26)?例如:

string str="hello";必须将其更改为"85121215",然后更改为85121215。

4 个答案:

答案 0 :(得分:3)

// transformation itself doesn't care what encoding we use
std::string transform_string(std::string const &in, std::function<int(char)> op)
{
    std::ostringstream out;
    std::transform(in.begin(), in.end(),
                   std::ostream_iterator<int>(out),
                   op);
    return out.str();
}

// the per-character mapping is easy to isolate
int ascii_az_map(char ch)
{
    if (ch < 'a' || ch > 'z') {
        std::ostringstream error;
        error << "character '" << ch << "'=" << (int)ch
              << " not in range a-z";
        throw std::out_of_range(error.str());
    }
    return 1 + ch - 'a';
}

// so we can support other encodings if necessary
// NB. ebdic_to_ascii isn't actually implemented here
int ebcdic_az_map(char ch)
{
    return ascii_az_map(ebcdic_to_ascii(ch));
}

// and even detect the platform encoding automatically (w/ thanks to Phresnel)
// (you can still explicitly select a non-native encoding if you want)
int default_az_map(char ch)
{
    #if ('b'-'a' == 1) && ('j' - 'i' == 1)
        return ascii_az_map(ch);
    #elif ('j'-'i' == 8)
        return ebcdic_az_map(ch);
    #else
        #error "unknown character encoding"
    #endif
}

// use as:
std::string str = "hello";
std::string trans = transform_string(str, ascii_az_map);
// OR ... transform_string(str, ebcdic_az_map);

请注意,由于每个字符的映射是完全隔离的,因此很容易将映射更改为查找表,支持不同的编码等。

答案 1 :(得分:1)

你的定义有点小:

"hello" = "85121215
h = 8
e = 5
l = 12
o = 15

我认为你的意思是

a = 1
b = 2
...
z = 26

在这种情况下并不难:

std::string meh_conv(char c) {
    switch(c) { // (or `switch(tolower(c))` and save some typing)
    case 'a': case 'A': return "1";
    case 'b': case 'B': return "2";
    ....
    case 'z': case 'Z': return "26";
    ....
    // insert other special characters here
    }
    throw std::range_error("meh");
}

std::string meh_conv(std::string const &src) {
    std::string dest;
    for (const auto c : s)
       dest += meh_conv(c);
    return dest;
}

或使用std::transform()

#include <algorithm>

std::string dest;
std::transform (src.begin(), src.end(), back_inserter(dest),
                meh_conv)

<击> (不适用于不同的传入和传出类型,至少不是这样)


附录

您可能希望参数化替换地图:

std::map<char, std::string> repl;
repl['a'] = repl['A'] = "0";
repl[' '] = " ";


std::string src = "hello";
std::string dest;
for (const auto c : src) dest += repl[c];

答案 2 :(得分:0)

我写了一个简单的例子。它创建一个包含a-1,b-2,c-3 ...对的地图。然后使用stringstream连接值:

#include <iostream>
#include <map>
#include <sstream>

int main()
{
    std::string str = "abc";
    std::map<char,int> dictionary;
    int n = 1;
    for(char c='a'; c<='z'; c++)
        dictionary.insert(std::pair<char,int>(c,n++));

    //EDIT if you want uppercase characters too:
    n=1;
    for(char c='A'; c<='Z'; c++)
        dictionary.insert(std::pair<char,int>(c,n++));        

    std::stringstream strstream;

    for(int i=0; i<str.size(); i++)
        strstream<<dictionary[str[i]];

    std::string numbers = strstream.str();

    std::cout<<numbers;

    return 0;
}

C ++专家可能会因为这个解决方案而杀了我,但它确实有效;)

答案 3 :(得分:0)

简易方法, 你可以找到96的char模型(a之前的ASCII值),因此它总是会给你1-26的值。

int value;
string s;
cin>>s;
for(int i=0; i<s.size();i++){
    value = s[j]%96;
    cout<<value<<endl;

}