How to Compose Digits into Integer Descriptions?

Click For Summary
SUMMARY

This discussion focuses on creating a program that reads digits as characters and converts them into a descriptive integer format. The initial approach involved converting characters to integers and using a switch statement, which was deemed inefficient. A more effective method proposed includes using an array of place names (e.g., "ones", "tens", "hundreds") and iterating through the input string in reverse to generate the output. The Perl script provided demonstrates this approach, emphasizing input sanitization and proper pluralization based on the digit values.

PREREQUISITES
  • Understanding of string manipulation in programming
  • Familiarity with arrays and loops in Perl
  • Basic knowledge of input validation techniques
  • Experience with conditional statements for output formatting
NEXT STEPS
  • Learn advanced string manipulation techniques in Perl
  • Explore Perl's array functions and their applications
  • Research input validation methods to handle various character types
  • Study conditional logic for dynamic output formatting in programming
USEFUL FOR

Programmers, especially those working with Perl, who are interested in string processing, input validation, and generating formatted outputs from user input.

darkvalentine
Messages
11
Reaction score
0
Object: Write a program that read digits and composes them into integers. For example, when you read 123( in characters) the program should print: "123(in integers) is 1 hundred and 2 tens and 3 ones".

My first attempt is first read the number as a string then convert its characters to integers then store them in another int vector. To print the result, I used switch to determine how many items are there in the vector (i.e if read 1234 (4 items) then print 1 thousand 2 hundred...) but then I realize it not a good way. How about if people read in 10 characters??

Please give me some idea to start.
 
Technology news on Phys.org
darkvalentine said:
Object: Write a program that read digits and composes them into integers. For example, when you read 123( in characters) the program should print: "123(in integers) is 1 hundred and 2 tens and 3 ones".

My first attempt is first read the number as a string then convert its characters to integers then store them in another int vector. To print the result, I used switch to determine how many items are there in the vector (i.e if read 1234 (4 items) then print 1 thousand 2 hundred...) but then I realize it not a good way. How about if people read in 10 characters??

Please give me some idea to start.

Don't ask homework questions :P

Proceeding along your original route of storing the char input and transforming it into an int vector:

Instead of switch/case, I'd create an array/vector of strings, with each string having the name "ones","tens","hundreds",...

Then just create a loop that runs backwards from the length of the input string down.

A couple details: you can determine how big an array is using sizeof( yourArray ). You might also want to do a preliminary parse to remove things like commas from the input.
 
darkvalentine said:
Object: Write a program that read digits and composes them into integers. For example, when you read 123( in characters) the program should print: "123(in integers) is 1 hundred and 2 tens and 3 ones".

My first attempt is first read the number as a string then convert its characters to integers then store them in another int vector. To print the result, I used switch to determine how many items are there in the vector (i.e if read 1234 (4 items) then print 1 thousand 2 hundred...) but then I realize it not a good way. How about if people read in 10 characters??

Please give me some idea to start.

Here we go:

Code:
#!/usr/bin/perl
$_=<>;
s/\D+//gs;
print "$_ (in integers) is";
@_=split //;
for(0..$#_) { print " ".(($_>0)?"and ":"")."$_[$_] ".(qw/one ten hundred thousand ten-thousand hundred-thousand million ten-million hundred-million billion ten-billion hundred-billion trillion ten-trillion hundred-trillion quadrillion/)[$#_-$_] . (($_[$_]==1)?"":"s"); }
print "\n";

Seriously, though:

1) Homework = not here, see the sticky.
2) Clean your input (make sure there are no other characters like decimal points, commas, other characters, etc.)
3) Make one array of names (as suggested above), like "one", "ten", "hundred", etc. However, note: there's no "s" on the end. There's a difference when you print them between saying "5 hundreds", "1 hundred", or "0 hundreds". Only print an "s" after the place's name if the value is 1!
4) Make a second array of the characters that were input
5) Loop through the character array normally, but loop through the "place name" array differently-- start in the middle (where appropriate), and work backwards, rather than forwards.

DaveE
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
8K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K