Tuesday, July 28, 2009

Turbo C problem?

Write a program that takes an integer as input and displays it in words.


Example


Input:Enter integer between 0 to 5000


30245


Output:Thirty thousand two hundred and fourty five

Turbo C problem?
convert the integer input into characters. take the note of the length of the input. then starting from the first element step through the array.





then once you find a number that is not zero print accordingly its values.





for example:





input %26gt; 543





get length. length = 3;


get first element '5';


get second element '4';


since first is different from second and since length is equal to 3. print '5' in hundreds place then print 'hundred'.





then get third element '3';


since again different from second element, print '4' in tens place then print 'forty' //forty is '4' in the tens place.





then since 3 is last element print '3' in one's place. '3' in one's place is 'three';





another example:


input %26gt; 6043


length = 4;


first element = '6'; second = '0';


print '6' since in thousand's place, print 'thousand';


since second = '0' get third element and print


'4' in tens place prints 'forty'


get last element '3';


print since in one's place, just print it in words. (three)
Reply:Here's one approach. Make a couple arrays of strings. They'll let you index the strings that correspond to the digits in the number to print.





char* digits[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}





char* teens[] = {"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}





char* tens[] = {"ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"}





The idea is that we can break the number into groups of three from right to left. Each group can be handled roughly the same way. Then you can write a function like f() here for each group of three digits. Say you have the number GHI where G,H,I are all integers in the range 0 to 9.





If G isn't 0, then you're going to say something like "G hundred". If it is, then you don't say anything about "hundred". If H isn't 0, then you're going to say something like "ten" or "twenty" or "thirty" if H is 1,2, or 3. If I isn't 0, then you're going to say something like "one", "two", or "three". We have to handle the teens as a special case. That's when H is 1 and I isn't 0.





Here's how we might do this:





void f(int G, int H, int I) {


if (G != 0) {


printf("%s hundred and ", digits[G]);


}


if (H == 0) {


printf("%s", digits[I]);


} else {


if (I == 0) {


printf("%s", tens[H-1]);


}


if (H == 1) {


printf("%s", teens[I-1]);


} else {


printf("%s %s", tens[H-1], digits[I]);


}


}


}





Then the following calls print out:


f(0,0,0) =%26gt; "zero"


f(0,0,3) =%26gt; "three"


f(0,1,3) =%26gt; "thirteen"


f(0,2,3) =%26gt; "twenty three"


f(1,2,3) =%26gt; "one hundred and twenty three"





You can adapt f() to do the next group of three from right to left as well. Just add a "thousand" to the final printf and you're set.


No comments:

Post a Comment