开发者

Why does my string get empty values appended to it when subtracting characters?

I am attempting to solve a problem from topcoder.com and it's driving me crazy. I am learning C++ after a long break from C and am having trouble with strings.

The purpose of the program is to decode a string of 0s and 1s that has gone through an encryption algorithm that consists of adding each adjacent digit to the digit in question.

So 010111 becomes 112232 (LSB and MSB are considered to have zeros next to them). Below is my algorithm to decode the string:

#include <string>
#include <vector>
#include <iostream>

using namespace std;

class BinaryCode {
public:
    vector<string> decode(string message);
};

vector<string> BinaryCode::decode(string message) {
    vector<string> decoded(2);
    int i;

    string myTempString;
    myTempString.append("0");
    myTempString.append(1,message[0] - myTempString[0]);
    for(i=2; i<message.size(); i++) {
        myTempString.append(1,message[i-1] - myTempString[i-1] - myTempString[i-2]);
    }

    decoded[0] = myTempString;

    myTempString = "";
    myTempString.append("1");
    myTempString.append(1,message[0] - myTempString[0]);
    for(i=2; i<message.size(); i++) {
        myTempString.append(1, message[i-1] - myTempString[i-1] - myTempString[i-2]);
    }

    decoded[1] = myTempString;

    return decoded;
}

int main () {
    string message("123210122");
    BinaryCode *code = new BinaryCode;
    vector<string> result = code->decode(message);
    cout << "Decoded strings are "+result[0]+" 开发者_如何学编程and "+result[1];
    getchar();
    return 0;
}

The output is nonsense:

Decoded strings are 01


This is just a guess, since you don't show what output you're getting, but it looks like you're doing math on the character values and ending up with characters in the control range. For example, '1' - '0' is not '1' (character 49), it is 1, or Control-A. This is not printable and will typically be invisible in the output. Similarly, '1' + '2' is 49 + 50, or 99, which is 'c'. C++ is not going to magically convert these characters to integers for you. Hopefully this will give you the information you need to fix your code.


A character is an 8-bit integral type. It has the special property that, when printed, it will appear as the character that matches the ASCII value that it contains.

For example:

int valueAsInt = 65;
char valueAsChar = valueAsInt;
std::cout << valueAsChar << "\n";

valueAsInt = 'A';
std::cout << valueAsInt << "\n";

A
65

Take the value of the character literal '0'. This corresponds to the ASCII value 48. '1' is 49, etc.

If you subtract 48 from 49, you get 1. But that's not what you're looking for.

The ASCII value 1 corresponds to a non-printable character, called "start of heading". It was once used on old printers as a sort of markup. It would not print, but it would modify how further characters are printed.

When you subtract one numeric character from another, you get a delta, not a printable character. To turn this delta back into a printable character, you have to add it to a base character:

char value = '5' - '3';
value += '0';
std::cout << "5 - 3 = " << value << "\n";

5 - 3 = 2

So, your code such as message[0] - myTempString[0] must be changed to message[0] - myTempString[0] + '0' in order to work the way you intend it to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜