paradox64ce

Untitled

Mar 8th, 2022 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. // C++ program for the above approach
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. // Function to convert mobile numeric
  6. // keypad sequence into its equivalent
  7. // string
  8. void printSentence(string str)
  9. {
  10.     // Store the mobile keypad mappings
  11.     vector<string> nums
  12.         = { " 0", ",@", "ABCabc2", "DEFdef3", "GHIghi4",
  13.             "JKLjkl5", "MNOmno6", "PQRSpqrs7", "TUVtuv8",
  14.             "WXYZwxyz9" };
  15.  
  16.     // Traverse the string str
  17.     int i = 0;
  18.     while (str[i] != '\0') {
  19.  
  20.         // If the current character is
  21.         // '.', then continue to the
  22.         // next iteration
  23.         if (str[i] == '_') {
  24.             i++;
  25.             continue;
  26.         }
  27.  
  28.         // Stores the number of
  29.         // continuous clicks
  30.         int count = 0;
  31.  
  32.         // Iterate a loop to find the
  33.         // count of same characters
  34.         while (str[i + 1]
  35.             && str[i] == str[i + 1]) {
  36.  
  37.             // 2, 3, 4, 5, 6 and 8 keys will
  38.             // have maximum of 3 letters
  39.             if (count == 6
  40.                 && ((str[i] >= '2'
  41.                     && str[i] <= '6')
  42.                     || (str[i] == '8')))
  43.                 break;
  44.  
  45.             // 7 and 9 keys will have
  46.             // maximum of 4 keys
  47.             else if (count == 8
  48.                     && (str[i] == '7'
  49.                         || str[i] == '9'))
  50.                 break;
  51.             count++;
  52.             i++;
  53.  
  54.             // Handle the end condition
  55.             if (str[i] == '\0')
  56.                 break;
  57.         }
  58.  
  59.         // Check if the current pressed
  60.         // key is 7 or 9
  61.         if (str[i] == '7' || str[i] == '9') {
  62.             cout << nums[str[i] - 48][count % 9];
  63.         }
  64.  
  65.         // Else, the key pressed is
  66.         // either 2, 3, 4, 5, 6 or 8
  67.         else {
  68.             cout << nums[str[i] - 48][count % 7];
  69.         }
  70.         i++;
  71.     }
  72. }
  73.  
  74. // Driver Code
  75. int main()
  76. {
  77.     string str = "111";
  78.     printSentence(str);
  79.     return 0;
  80. }
  81.  
Add Comment
Please, Sign In to add comment