Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- string single_numberToWords(string num) {
- if (num == "1") {
- return "One";
- }
- if (num == "2") {
- return "Two";
- }
- if (num == "3") {
- return "Three";
- }
- if (num == "4") {
- return "Four";
- }
- if (num == "5") {
- return "Five";
- }
- if (num == "6") {
- return "Six";
- }
- if (num == "7") {
- return "Seven";
- }
- if (num == "8") {
- return "Eight";
- }
- if (num == "9") {
- return "Nine";
- }
- return "";
- }
- string double_numberToWords(string num) {
- if (num == "00") {
- return "";
- }
- if (num[0] == '0') {
- return single_numberToWords(num.substr(1, 1));
- }
- if(num == "10"){
- return "Ten";
- }
- if (num == "11") {
- return "Eleven";
- }
- if (num == "12") {
- return "Twelve";
- }
- if (num == "13") {
- return "Thirteen";
- }
- if (num == "14") {
- return "Fourteen";
- }
- if (num == "15") {
- return "Fifteen";
- }
- if (num == "16") {
- return "Sixteen";
- }
- if (num == "17") {
- return "Seventeen";
- }
- if (num == "18") {
- return "Eighteen";
- }
- if (num == "19") {
- return "Nineteen";
- }
- string to_return = "";
- if (num[0] == '2') {
- to_return += "Twenty";
- }
- if (num[0] == '3') {
- to_return += "Thirty";
- }
- if (num[0] == '4') {
- to_return += "Forty";
- }
- if (num[0] == '5') {
- to_return += "Fifty";
- }
- if (num[0] == '6') {
- to_return += "Sixty";
- }
- if (num[0] == '7') {
- to_return += "Seventy";
- }
- if (num[0] == '8') {
- to_return += "Eighty";
- }
- if (num[0] == '9') {
- to_return += "Ninety";
- }
- if (num[1] != '0') {
- to_return += " " + single_numberToWords(num.substr(1, 1));
- }
- return to_return;
- }
- string three_numberToWords(string num) {
- //num is of three digits
- string to_return = "";
- if (num[0] != '0') {
- to_return += single_numberToWords(num.substr(0, 1)) + " " + "Hundred";
- }
- if(!to_return.empty())
- to_return += " " + double_numberToWords(num.substr(1, 2));
- else
- to_return += double_numberToWords(num.substr(1, 2));
- return to_return;
- }
- string denomination(int index) {
- switch (index / 3) {
- case 0:
- return "";
- case 1:
- return " Thousand";
- case 2:
- return " Million";
- case 3:
- return " Billion";
- case 4:
- return " Trillion";
- case 5:
- return " Quadrillion";
- case 6:
- return " Quintillion";
- case 7:
- return " Sextillion";
- case 8:
- return " Septillion";
- case 9:
- return " Octillion";
- case 10:
- return " Nonillion";
- case 11:
- return " Decillion";
- case 12:
- return " Undecillion";
- }
- return "";
- }
- string numberToWords(int numInt) {
- if(numInt == 0) return "Zero";
- string num = "";
- while (numInt != 0) {
- num += to_string(numInt % 10);
- numInt /= 10;
- }
- reverse(num.begin(), num.end());
- cout << num << endl;
- vector < string > answer;
- int i = num.size() - 1;
- int valDeno = 0;
- while (i >= 0) {
- string chunk = "";
- for (int k = 0; k <= 2 && i >= 0; k++) {
- chunk += num[i];
- i--;
- valDeno++;
- }
- reverse(chunk.begin(), chunk.end());
- //now we've got the chunk
- if(chunk.size()==3 && !three_numberToWords(chunk).empty()){
- answer.push_back(three_numberToWords(chunk) + denomination(valDeno - 1));
- }
- else if(chunk.size()==2 && !double_numberToWords(chunk).empty()){
- answer.push_back(double_numberToWords(chunk) + denomination(valDeno - 1));
- }
- else if(chunk.size()==1 && !single_numberToWords(chunk).empty()){
- answer.push_back(single_numberToWords(chunk) + denomination(valDeno - 1));
- }
- }
- string to_return = "";
- for (int i = answer.size() - 1; i >= 0; i--) {
- if(i==0){
- to_return += answer[i];
- break;
- }
- if(answer[i].size()==0) continue;
- to_return += answer[i] + " ";
- }
- string to_return_removeSpace = "";
- for(int i=0; i<to_return.size(); i++){
- if(to_return[i]!=' '){
- to_return_removeSpace += to_return[i];
- }
- else{
- while(i < to_return.size() && to_return[i] == ' '){
- i++;
- }
- if(i == to_return.size()){
- break;
- }
- to_return_removeSpace += ' ';
- i--;
- }
- }
- return to_return_removeSpace;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement