Guest User

Untitled

a guest
Feb 24th, 2019
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. int toLower(int code){
  7.     return code + 32;
  8. };
  9.  
  10. int isCapital(int code) {
  11.     return (code >= 65 && code <= 90);
  12. };
  13.  
  14. int toUpper(int code) {
  15.     return code - 32;
  16. };
  17.  
  18. bool isAlpha(int code){
  19.     return (isCapital(code) || (code >= 97 && code <= 122));
  20. };
  21.  
  22. tuple<int, int> processChar(int code) {
  23.     tuple<int, int> returnVal;
  24.     bool const alpha = isAlpha(code);
  25.     if(alpha){
  26.         bool const capital = isCapital(code);
  27.         if(capital) {
  28.             int const lowerCaseChar = toLower(code);
  29.             returnVal = make_tuple(1, lowerCaseChar);
  30.         } else returnVal = make_tuple(0, code);
  31.     } else returnVal = make_tuple(-1, code);
  32.     return returnVal;
  33. };
  34.  
  35. void translate(string alphabet, string sen){
  36.     char transCode;
  37.     for (auto i = sen.begin(); i != sen.end(); i++) {
  38.  
  39.         int code = (*i);
  40.         tuple<int, int> charInfo = processChar(code);
  41.  
  42.         int alphaCheck, transformCode;
  43.         tie(alphaCheck, transformCode) = charInfo;
  44.  
  45.         if(alphaCheck >= 0){
  46.             int idx = transformCode - 'a';
  47.             transCode = alphabet[idx];
  48.             if(alphaCheck == 1) transCode -= 32;
  49.         } else if(transformCode == 95){
  50.             transCode = ' ';
  51.         }else transCode = transformCode;
  52.         cout << transCode;
  53.     }
  54. }
  55.  
  56. int main() {
  57.     ios_base::sync_with_stdio(0);
  58.     cin.tie(NULL);
  59.  
  60.     int tc;
  61.     cin >> tc;
  62.  
  63.     string balpha;// = "qwertyuiopasdfghjklzxcvbnm"; //= "zabcdefghijklmnopqrstuvwxy";
  64.     cin >> balpha;
  65.  
  66.     while (tc != 0) {
  67.  
  68.         string sen;
  69.         cin >> sen;
  70.  
  71.         translate(balpha, sen);
  72.         cout << "\n";
  73.         tc--;
  74.     }
  75.     return 0;
  76. }
Add Comment
Please, Sign In to add comment