(C ++)将字符串中找到的数组的任何值替换为其他数组的相应值?

时间:2012-11-22 16:59:09

标签: c++ arrays string char

我尝试过各种各样的事情。不幸的是,我没有保留以前的代码记录,所以我只会告诉你我目前的情况:

    string l2n(std::string input) {
    std::string original[26] = {"A","B","C","D","E", "F", "G", "H", "I", "J", "K," "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    //std::string originalLower[26] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    std::string newval[26] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"};
    string neworig = (original[0], original[1], original[2], original[3], original[4], original[5], original[6], original[7], original[8], original[9], original[10], original[11], original[12], original[13], original[14], original[15], original[16], original[17], original[18], original[19], original[20], original[21], original[22], original[23], original[24], original[25]);
    string newnewval = (newval[0], newval[1], newval[2], newval[3], newval[4], newval[5], newval[6], newval[7], newval[8], newval[9], newval[10], newval[11], newval[12], newval[13], newval[14], newval[15], newval[16], newval[17], newval[18], newval[19], newval[20], newval[21], newval[22], newval[23], newval[24], newval[25]);
    size_t posit = input.find(neworig.c_str());
    string hash2345;
    hash2345 = input.replace(posit, std::string(neworig).length(), newnewval.c_str());
    return hash2345;
}

编译好,但输入“HELLO”输出“26HELLO”。它确实只是在给出任何输入的开头附加26 ..我猜这与使用input.replace使用长度有关,但我不知道还有什么要做,谷歌没有找到任何东西。 谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

我认为你误解了string::replace的作用。它不会将一个字符串中的字符替换为另一个字符串中的等效字符。它将一个字符串中的一个字符替换为另一个字符串。

例如,

std::string test = "This is a test.";

// Replace 2 characters, starting at position 5 with "was"
test.replace(5, 2, "was");

std::cout << test;  // Outputs "This was a test."

对于你正在做的事情,你需要一个循环来对输入中的每个字符执行一个动作,例如

string hash2345;

// For each character in input
for (int i = 0; i < input.length(); ++i) {

  // Try to find the string equal to the i'th character of input in 
  // the "original" array.
  string* original_val_ptr = std::find(original, original + 26, 
                                       std::string(input[i]));

  // Make sure that it actually found something (this handles the case
  // where input has some other characters, like lowercase or punctuation).
  if (original_val_ptr != NULL) {

    // Find the position of this value in the original array.
    size_t pos = original_val_ptr - original;

    // Add the string in the equivalent position in the "newval" array
    // to the output.
    hash2345 += newval[pos];
  }
}

return hash2345;

现在可以对您的代码进行一些额外的改进,我确信其他答案会建议他们,但鉴于您似乎是一个相对较新的程序员,我选择保留这个答案尽可能简单明了,只关注你显然试图实现的算法的正确性。

此外,这适用于任何一组字符到另一组字符的映射,即使它不是数字字母。

答案 1 :(得分:0)

根据上面的评论,假设您使用的是ASCII,那么以下内容可以实现您想要的效果

string l2n(std::string input)
{
    ostringstream buf;
    for (string::const_iterator i = input.begin(); i != input.end() ++i)
    {
        buf << (*i - 'A') + 1;
    }
    return buf.str();
}