-
Notifications
You must be signed in to change notification settings - Fork 72
/
Convert_digits_numbers_to_words.cpp
55 lines (53 loc) · 1.31 KB
/
Convert_digits_numbers_to_words.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
using namespace std;
void convertnumintoword(string str)
{
string singledigit[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
string twodigits[] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "ninteen", "twelve"};
string tensmultiple[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninty"};
string tenspower[] = {"hundred", "thousand"};
if (str.length() == 0)
{
cout << "" << endl;
return;
}
else if (str.length() == 1)
{
cout << singledigit[str[0] - '0'] << endl;
return;
}
int len = str.length();
for (int i = 0; i < str.length(); i++)
{
if (len > 2)
{
if (str[i] - '0' != 0)
{
cout << singledigit[str[i] - '0'] << " ";
cout << tenspower[len - 3] << " ";
}
len--;
}
else
{
if (str[i] - '0' == 1)
{
cout << twodigits[str[i + 1] - '0'] << " ";
return;
}
else if (str[i] - '0' != 0)
{
cout << tensmultiple[str[i] - '0'] << " ";
if (str[i + 1] - '0' != 0)
cout << singledigit[str[i + 1] - '0'] << " ";
return;
}
}
}
}
int main()
{
string str = "9090";
convertnumintoword(str);
return 0;
}