简体   繁体   English

将一个数组中的字符替换为另一个数组中的字符

[英]replace characters from one array to another to a string

I expected this to be straightforward using a for loop with replace(str.begin(), str.end(), "x", "y") but instead "x" and "y" are a certain character in an array, so: replace(str.begin(), str.end(), arrX[1], arrY[1]) in the loop: 我期望使用带有replace(str.begin(), str.end(), "x", "y")的for循环replace(str.begin(), str.end(), "x", "y")而是"x""y"是数组中的某个字符,所以:在循环中replace(str.begin(), str.end(), arrX[1], arrY[1])

for (int i = 0; i < arraySize; i++) {
    replace( str.begin(), str.end(), arrX[i], arrY[i]);
}

but in my code: 但在我的代码中:

#include <iostream>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

string Caesar(string str) { 
  char alph[]   = {'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'};
  string caesar = "abcdefghijklmnopqrstuvwxyz";
  const int arraySize=sizeof(alph)/sizeof(alph[0]);
  rotate(caesar.begin(), caesar.begin()+3, caesar.end());
  for (int i = 0; i < arraySize; i++) {
    replace(str.begin(), str.end(), alph[i], caesar[i]);
  }
  return str; 
}
int main() {
  cout << Caesar("hello");
}

caeser string is alph rotated by three. caeser字符串是alph旋转三。

Outputting caesar gives expected results. 输出凯撒可以得到预期的结果。 (abcdef... becomes xyzabc... when just printing caesar.) (abcdef ...变成xyzabc ......只是印刷凯撒。)

My loop seems to be the thing messing it up, when given hello it produces ccaaa . 我的循环似乎是弄乱它的东西,当给出hello它产生ccaaa I tested replacing one letter and it worked but it seems my for loop is what the problem is, but I can't seem to find out what is wrong. 我测试过替换一个字母并且它有效但看起来我的循环问题是什么,但我似乎无法找出问题所在。

UPDATE: 更新:

I found out a way to do it that supports non alphabetical characters using a while loop that checks if it is alphabetical and then goes through the alphabet comparing each letter to a letter in the string until they match and than replace that with the rotated Caesar alphabet, if they don't match it goes to the next one and when found it resets 'j' to 0 so it can do it again for the next letter by increment 'i' this time, if the char is not a letter it just increases 'i' to the next char to just skip over it until it reaches the new letter or the end of the string. 我找到了一种方法来支持非字母字符,使用while循环检查它是否是按字母顺序排列,然后通过字母表比较每个字母与字符串中的字母,直到它们匹配,然后用旋转的凯撒字母替换它,如果它们不匹配则转到下一个并且当发现它将'j'重置为0时所以它可以通过增加'i'再次为下一个字母再次执行它,如果char不是它只是一个字母将'i'增加到下一个char只是跳过它直到它到达新字母或字符串的结尾。

#include <iostream>
#include <algorithm>

using namespace std;

bool IsInArray(string array, char element) {
  for(int i = 0; i < array.length(); i++){
     if(array[i] == element){
         break;
     }
  }
}
string rot(int n, string str) {
  transform(str.begin(), str.end(), str.begin(), ::tolower);
  string alph = "abcdefghijklmnopqrstuvwxyz";
  string ciph = alph;
  rotate(ciph.begin(), ciph.begin() + n, ciph.end());

  int i = 0;
  int j = 0;
  while (i < str.length()) {
    if (IsInArray(alph, str[i])) {
      if (str[i] == alph[j]) {
        str[i] = ciph[j];
        i++;
        j = 0;
      }
      else {
        j++;
      }
    }
    else {
      i++;
    }
  }
  return str;
}

int main() {
  cout << rot(2, "This cipher works with more than just alpha chars!");
  return 0;
}

Here's one way to do it using standard functions and a lambda function: 这是使用标准函数和lambda函数执行此操作的一种方法:

string Caesar(std::string str) { 
    std::string caesar = "abcdefghijklmnopqrstuvwxyz";
    std::rotate(caesar.begin(), caesar.end()-3, caesar.end());
    std::transform(str.begin(), str.end(), str.begin(), 
        [caesar](char c) -> char { return caesar[c - 'a']; });
    return str; 
}

Note: it uses the char code to get the index, so it would have to be changed to handle anything other than "abc...xyz". 注意:它使用char代码来获取索引,因此必须更改它以处理除“abc ... xyz”之外的任何内容。

As an optimization: if you're using just letters az lowercase, you can do it without using the two arrays and without using the function leftRotatebyOne . 作为优化:如果你只使用字母az小写,你可以不使用两个数组而不使用函数leftRotatebyOne Instead use the ASCII values. 而是使用ASCII值。

std::string Caesar(std::string str){
    int delta = 'z' - 'a' + 1;
    int rotateBy = 3;
    for(int i = 0; i < str.length(); i++){
        char c = str[i] - rotateBy;
        if(c < 'a')
            c += delta;
        str[i] = c;
    }
    return str;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM