/ Published in: C++
Thing for ASCII Addition
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#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; }