ASCII Addition in C++


/ Published in: C++
Save to your folder(s)

Thing for ASCII Addition


Copy this code and paste it in your HTML
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. // Inits the two strings and the string to store the answer;
  9. string string1 = "HI I LIKE PIE", string2 = "ANDREW", answer = "";
  10. // Gets the strings from the terminal
  11. //cin >> string1 >> string2;
  12. // Gets the index of the last character in each string
  13. int lastInString1 = string1.length() - 1;
  14. int lastInString2 = string2.length() - 1;
  15. // Makes a loop that goes from 0 to the longest string's length
  16. for (int iterator = 0; iterator <= max(lastInString1, lastInString2); iterator++) {
  17. int sum = 0;
  18. // Checks to make sure that the string has an index for the character we want
  19. if (lastInString1 - iterator >= 0) sum += string1[lastInString1 - iterator];
  20. if (lastInString2 - iterator >= 0) sum += string2[lastInString2 - iterator];
  21. // MOD's the sum and makes sure its not a special character
  22. sum = sum % 127;
  23. if (sum < 32) sum = 32;
  24. // Puts the new character at the begining of the string because we are going in reverse
  25. answer = (char)sum + answer;
  26. }
  27. cout << answer << endl;
  28. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.