``````void numToEnglish(unsigned int num, char english[]);
main()
{
char* first20[] = {"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen","nineteen"};
char* tens[] = {"twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety"};

printf("Enter an integer between 0 and 999: ");
scanf("%s",s1);
printf("This number in English is &s",s1);

}``````

Hey guys, I am learning how to program with C for the first time. I need to write a program that uses string processing and will convert input numbers to English phrases.
Example:
Enter an integer between 0 and 999: 375
This number in English is three hundred seventy five

I have an idea with this program. I get an input, store it in s1.
strlen to check how long s1 is and then that would let me know if
its in the hundreds,tens, or ones. I don't know how to tackle this program
any further. Can I have some help?

All 5 Replies

You could store the number in an array then check each index for the English word

you can use an if else condition for every number from zero to nine in comparing

I have an idea with this program. I get an input, store it in s1.
strlen to check how long s1 is and then that would let me know if
its in the hundreds,tens, or ones.

That's a good approach. If you know what the digit is and what place it's in, the only slightly tricky part is handling paired numbers like the teens so that your output is correct.

You will also find it easier if you fill out the tens array so you don't have to do any math to get the correct subscript:

``````char* tens[] = {"---", "ten", "twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety"};``````

Now when you get the tens digit 2 it maps directly to twenty.

``````void numToEnglish(unsigned int num, char english[]);
main()
{
char s2[40];

int y, num;

printf("Enter an integer between 0 and 999: ");
scanf("%d",num);

y = numToEnglish(num);

s2 = y;

printf("This number in English is &s",s2);

}

numToEnglish(unsigned int num, char english[])
{
char* first20[] = {"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen","nineteen"};
char* tens[] = {"twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety"};
char s1[40];

int test,hundred;

test = num;

if(test>=100)
{
hundred = test/100;
}
else
{
hundred = test;
}
strcopy(s1,first20[hundred-1];

return hundred;
}``````

I have read everyone's post. I have to make this a function using char. So I thought of moving the char into the function and calculate the place of the first number. Then strcopy so that I can then return a word value back to main. I am taking this step by step. So I just want to try if I can even get the hundredths place to work. Then go from there.

I am just a little confused with the function prototype and how to get it to work.

``void numToEnglish(unsigned int num, char english[]);``

I am just a little confused with the function prototype and how to get it to work.

``void numToEnglish(unsigned int num, char english[]);``

Since it makes sense to us, you need to explain what's confusing you. We are notoriously bad guessers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.