Reputation: 25
There is a code that I need to transform a number that the user types into a word, example : 123 = one, two, three. How can I continue this code?
int main()
{
int number, count;
char numberWritten[] = {"Zero" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine"};
printf("Type a number: ");
scanf("%d" , &number);
for (i = 0; i<=9; i++)
{
printf("")
}
}
I´d like to add that i am a newbie, that I just started learning about strings and that I don´t know what void is or where to put it.
Upvotes: 0
Views: 1106
Reputation: 67855
void printnumber(unsigned number)
{
static const char * const numberWritten[] = {"Zero" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine"};
if(number/10)
printnumber(number / 10);
printf("%s ", numberWritten[number % 10]);
}
or
void printnumber(int number)
{
static const char * const numberWritten[] = {"Zero" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine"};
if(number/10)
printnumber(number / 10);
else
if(number < 0 && number > -10) printf("minus ");
printf("%s ", numberWritten[abs(number % 10)]);
}
or without recursion
void printnumber(int number)
{
int mask = 1;
static const char * const numberWritten[] = {"Zero" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine"};
while(abs(number / mask) > 10) mask *= 10;
if(number < 0) printf("minus ");
for(; mask; mask /= 10)
printf("%s ", numberWritten[abs(number / mask) % 10]);
}
Upvotes: 0
Reputation: 123568
Your declaration for numberWritten
is slightly wrong - it should be
// +---- Need unary * here
// |
// V
char *numberWritten[] = {"Zero" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine"};
The char
type represents a single character, not a string. In C, a string is a sequence of character values including a zero-valued terminator, so you need an array of char
to store a string:
char foo[] = "One"; // equivalent to char foo[] = {'O', 'n', 'e', 0};
// equivalent to char foo[4]; foo[0] = 'O'; foo[1] = 'n'; etc.
If you don't specify the number of elements in a declaration, the array size is taken from the number of elements in the initializer. The string "One"
is equivalent to the sequence {'O', 'n', 'e', 0}
, which is 4 elements.
Since you want to store an array of strings, you either need to declare numberWritten
as an array of arrays of char
:
// The longest strings are 5 characters long, so each
// individual string needs an array of 6 characters to account
// for the string terminator
char numberWritten[][6] = { "One", "Two", ... };
OR
you need to declare an array of pointers to char:
char *numberWritten[] = { "One", "Two", ... };
Each string literal is itself an array expression, and in most circumstances array expressions "decay" to pointer expressions. Since you probably don't want those strings to change, it would be safer to declare that as
const char *numberWritten[] = { "One", "Two", ... };
That way you can't accidentally update the contents of each numberWritten[i]
.
As far as printing these out, you're very close. Just remember the following:
numberWritten[0] == "Zero"
, numberWritten[1] == "One"
, etc. IOW, each numberWritten[i]
(hint hint hint) corresponds to the word for the i
'th value.
printf
, use the %s
conversion specifier. Example:char foo[] = "this is a string";
printf( "%s\n", foo ); // writes "this is a string" followed by a newline to standard output
Upvotes: 1