Return to Snippet

Revision: 38185
at December 28, 2010 10:12 by mattloto


Initial Code
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main() {
    // Inits the two strings and the string to store the answer;
    string string1 = "HI I LIKE PIE", string2 = "ANDREW", answer = "";
    // Gets the strings from the terminal
    //cin >> string1 >> string2;
    // Gets the index of the last character in each string
    int lastInString1 = string1.length() - 1;
    int lastInString2 = string2.length() - 1;
    // Makes a loop that goes from 0 to the longest string's length
    for (int iterator = 0; iterator <= max(lastInString1, lastInString2); iterator++) {
        int sum = 0;
        // Checks to make sure that the string has an index for the character we want
        if (lastInString1 - iterator >= 0) sum += string1[lastInString1 - iterator];
        if (lastInString2 - iterator >= 0) sum += string2[lastInString2 - iterator];
        // MOD's the sum and makes sure its not a special character
        sum = sum % 127;
        if (sum < 32) sum = 32;
        // Puts the new character at the begining of the string because we are going in reverse
        answer = (char)sum + answer;
    }
    cout << answer << endl;
}

Initial URL


Initial Description
Thing for ASCII Addition

Initial Title
ASCII Addition in C++

Initial Tags


Initial Language
C++