Advertisement
HjHimansh

Integer to Words

Aug 24th, 2020
1,418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.75 KB | None | 0 0
  1. class Solution {
  2.     public:
  3.  
  4.         string single_numberToWords(string num) {
  5.             if (num == "1") {
  6.                 return "One";
  7.             }
  8.             if (num == "2") {
  9.                 return "Two";
  10.             }
  11.             if (num == "3") {
  12.                 return "Three";
  13.             }
  14.             if (num == "4") {
  15.                 return "Four";
  16.             }
  17.             if (num == "5") {
  18.                 return "Five";
  19.             }
  20.             if (num == "6") {
  21.                 return "Six";
  22.             }
  23.             if (num == "7") {
  24.                 return "Seven";
  25.             }
  26.             if (num == "8") {
  27.                 return "Eight";
  28.             }
  29.             if (num == "9") {
  30.                 return "Nine";
  31.             }
  32.             return "";
  33.         }
  34.  
  35.     string double_numberToWords(string num) {
  36.         if (num == "00") {
  37.             return "";
  38.         }
  39.         if (num[0] == '0') {
  40.             return single_numberToWords(num.substr(1, 1));
  41.         }
  42.        
  43.         if(num == "10"){
  44.             return "Ten";
  45.         }
  46.         if (num == "11") {
  47.             return "Eleven";
  48.         }
  49.         if (num == "12") {
  50.             return "Twelve";
  51.         }
  52.         if (num == "13") {
  53.             return "Thirteen";
  54.         }
  55.         if (num == "14") {
  56.             return "Fourteen";
  57.         }
  58.         if (num == "15") {
  59.             return "Fifteen";
  60.         }
  61.         if (num == "16") {
  62.             return "Sixteen";
  63.         }
  64.         if (num == "17") {
  65.             return "Seventeen";
  66.         }
  67.         if (num == "18") {
  68.             return "Eighteen";
  69.         }
  70.         if (num == "19") {
  71.             return "Nineteen";
  72.         }
  73.  
  74.         string to_return = "";
  75.         if (num[0] == '2') {
  76.             to_return += "Twenty";
  77.         }
  78.         if (num[0] == '3') {
  79.             to_return += "Thirty";
  80.         }
  81.         if (num[0] == '4') {
  82.             to_return += "Forty";
  83.         }
  84.         if (num[0] == '5') {
  85.             to_return += "Fifty";
  86.         }
  87.         if (num[0] == '6') {
  88.             to_return += "Sixty";
  89.         }
  90.         if (num[0] == '7') {
  91.             to_return += "Seventy";
  92.         }
  93.         if (num[0] == '8') {
  94.             to_return += "Eighty";
  95.         }
  96.         if (num[0] == '9') {
  97.             to_return += "Ninety";
  98.         }
  99.  
  100.         if (num[1] != '0') {
  101.             to_return += " " + single_numberToWords(num.substr(1, 1));
  102.         }
  103.         return to_return;
  104.     }
  105.  
  106.     string three_numberToWords(string num) {
  107.         //num is of three digits
  108.         string to_return = "";
  109.         if (num[0] != '0') {
  110.             to_return += single_numberToWords(num.substr(0, 1)) + " " + "Hundred";
  111.         }
  112.         if(!to_return.empty())
  113.             to_return += " " + double_numberToWords(num.substr(1, 2));
  114.         else
  115.             to_return += double_numberToWords(num.substr(1, 2));
  116.         return to_return;
  117.     }
  118.  
  119.     string denomination(int index) {
  120.         switch (index / 3) {
  121.         case 0:
  122.             return "";
  123.         case 1:
  124.             return " Thousand";
  125.         case 2:
  126.             return " Million";
  127.         case 3:
  128.             return " Billion";
  129.         case 4:
  130.             return " Trillion";
  131.         case 5:
  132.             return " Quadrillion";
  133.         case 6:
  134.             return " Quintillion";
  135.         case 7:
  136.             return " Sextillion";
  137.         case 8:
  138.             return " Septillion";
  139.         case 9:
  140.             return " Octillion";
  141.         case 10:
  142.             return " Nonillion";
  143.         case 11:
  144.             return " Decillion";
  145.         case 12:
  146.             return " Undecillion";
  147.         }
  148.  
  149.         return "";
  150.     }
  151.  
  152.     string numberToWords(int numInt) {
  153.         if(numInt == 0) return "Zero";
  154.         string num = "";
  155.         while (numInt != 0) {
  156.             num += to_string(numInt % 10);
  157.             numInt /= 10;
  158.         }
  159.         reverse(num.begin(), num.end());
  160.         cout << num << endl;
  161.         vector < string > answer;
  162.         int i = num.size() - 1;
  163.  
  164.         int valDeno = 0;
  165.         while (i >= 0) {
  166.             string chunk = "";
  167.             for (int k = 0; k <= 2 && i >= 0; k++) {
  168.                 chunk += num[i];
  169.                 i--;
  170.                 valDeno++;
  171.             }
  172.             reverse(chunk.begin(), chunk.end());
  173.             //now we've got the chunk
  174.             if(chunk.size()==3 && !three_numberToWords(chunk).empty()){
  175.                 answer.push_back(three_numberToWords(chunk) + denomination(valDeno - 1));
  176.             }
  177.             else if(chunk.size()==2 && !double_numberToWords(chunk).empty()){
  178.                 answer.push_back(double_numberToWords(chunk) + denomination(valDeno - 1));
  179.             }
  180.             else if(chunk.size()==1 && !single_numberToWords(chunk).empty()){
  181.                 answer.push_back(single_numberToWords(chunk) + denomination(valDeno - 1));
  182.             }
  183.         }
  184.  
  185.         string to_return = "";
  186.         for (int i = answer.size() - 1; i >= 0; i--) {
  187.             if(i==0){
  188.                 to_return += answer[i];
  189.                 break;
  190.             }
  191.             if(answer[i].size()==0) continue;
  192.             to_return += answer[i] + " ";
  193.         }
  194.         string to_return_removeSpace = "";
  195.         for(int i=0; i<to_return.size(); i++){
  196.             if(to_return[i]!=' '){
  197.                 to_return_removeSpace += to_return[i];
  198.             }
  199.             else{
  200.                 while(i < to_return.size() && to_return[i] == ' '){
  201.                     i++;
  202.                 }
  203.                 if(i == to_return.size()){
  204.                     break;
  205.                 }
  206.                 to_return_removeSpace += ' ';
  207.                
  208.                 i--;
  209.             }
  210.         }
  211.         return to_return_removeSpace;
  212.     }
  213. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement