0

I've tried all kinds of things. Unfortunately, I don't keep previous records of code so I'll just show you what I have currently:

    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;
}

That compiles fine, but outputs "26HELLO" with my input "HELLO". It literally just appends 26 to the beginning of any input given..I'm guessing it has something to do with the usage of input.replace using the length, but I don't know what else to do and google isn't finding anything. Thanks for your help!

6
  • On the 5th and 6th lines, where you assign to the strings neworig and newnewval respectively, what is it that you think those lines are supposed to be doing? Commented Nov 22, 2012 at 17:03
  • Oh, that's just left over from an old record. My bad. They should be, well, not there and any usage replaced with original and newval. I'll edit my post. Edit: Nevermind. It's been a night since I coded this, so it slipped my mind. the c_str() function doesn't work on original and newval for some reason, so I wrapped them in new strings. Commented Nov 22, 2012 at 17:04
  • 1
    What do you actually want to happen? You say that 26HELLO is wrong, but what would be the right result? It's not clear at the moment. Commented Nov 22, 2012 at 17:06
  • The end result SHOULD replace all the letters in the input with numbers. E.g, A = 1, B = 2, C = 3....Z = 26 Commented Nov 22, 2012 at 17:07
  • Well, if that's the case, you should be informed that the value of neworig is the value of original[25], and the value of newnewval is the value of newval[25]. Look up the comma operator. Did you mean to concatenate the strings together? Then use operator+ or operator+=. Also, a for loop. Commented Nov 22, 2012 at 17:11

2 Answers 2

1

I think you are misunderstanding what string::replace does. It does not replace characters in one string with the equivalently positioned characters in another. It replaces a span of characters in one string with the entirety of another string.

For example,

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."

For what you are doing, you will need a loop that performs an action on each character in your input, e.g.

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;

Now there are a number of additional improvements that can be made to your code, and I'm sure that other answers will suggest them, but given that you seem to be a relatively new programmer, I am choosing to keep this answer as straightforward as possible and focus only on the correctness of the algorithm that you are apparently trying to implement.

Also, this will work for any mapping of one set of characters to another, even if it is not letters to numbers.

Sign up to request clarification or add additional context in comments.

Comments

0

Based on the comments above, and assuming you are using ASCII, then the following does what you want

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.