takes an integer 32 as input and displays "thirty two" as output
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Eh, as always, in every language, coding is easy
Figuring out how to make the code do what you want is what turns your hair grey.
That's probably what his teacher is looking for, in which case we just did most of his homework.
@Habitual
In shell, user input can be acomplished on the command line. He doesn't say "ask for user input", he says "create a bash script that takes an integer"
which I'm interpreting to mean "./myscript.sh 32 " would suffice. Global/built in var $1 would then contain the number 32
I do not even know why I jumped in on this alleged invalid homework question. Oh yeah, Just sharing my thoughts on it like everyone else. .. shutting up on this question :mode: ON
Goodness, don't do that.
You looked at this problem and offered an opinion
Someone else looked at it differently and disagreed
We could very well be in reverse positions next time
and since the purpose of this board is to provide knowledge and understanding, your participation in the conversation is invaluable (or at the very least, just as valuable as mine).
I'd like to remind everyone that while it is fair to note to a new poster that they have asked a poor question, as advice givers we all should be practicing a few things:
Restraint with the unhelpful and critical comments
We should welcome the new poster and give them information related to how best to re-shape their question
We should perhaps offer some links relevant to their task at hand
Members should consider being silent and allow a first time poster to have the time to respond back with updates. Another reason for this is that sometimes there are new members who ask for direct help by way of asking for full code and support and when they're informed that the LQ site does not work in this manner, they will no longer respond. This can help avoid frustration on the part of advice givers, should avoid continued negative comments which serve no one, and in the possible event that max-shah just hasn't gotten back to respond as yet, the fewer insults or negative comments, the better the picture may be for when they decide to put forth some effort to show their attempts or update their question with more details
And this if @max-shah the OP should ever come back, should be enough to get him started on his path of self knowledge about BASH Scripting.
Have fun
Code:
#!/bin/bash
#Bash script to convert numbers into words
function Covert_num_2_words {
chrlen="$number"
if [[ ${#chrlen} -eq 0 ]] ; then
printf "Empty String\n"
retutn 1
fi
if [[ ${#chrlen} -gt 4 ]] ; then
printf "Length greater than 4 not supported\n"
return 1
fi
single_digits=("zero" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine")
two_digits=("" "ten", "eleven" "twelve" "thirteen" "fourteen" "fifteen" "sixteen" "seventeen" "eighteen" "nineteen")
ten_multiple=("" "" "twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty" "ninety")
ten_power=("hundred" "thousand")
#debugging
printf "\n Number: $number\n"
printf "${#chrlen}\n"
if [[ ${#chrlen} == 1 ]] ;then
printf "single_digits[$number - 0]\n"
printf ""${single_digits[$number]}"\n"
fi
}
read -p 'Enter a number ' number
Covert_num_2_words number
Remember Arrays are zero based.
results;
Quote:
(userx@SlackO⚡️~/test)>>$ ls
ConvertNumbersIntoWords a.out dependencies h homework1 main.cpp
(userx@SlackO⚡️~/test)>>$ ./ConvertNumbersIntoWords
Enter a number 7
A creative solution would be to pass the number to a text-to-speech program, and then pipe the resulting audio to a speech-to-text program configured to use words for numbers.
right then set it up to speak to a voice recognition type writer and have it type it out then send it to a scanner for processing with word recognition software and have it send that to a program that can store it in a file for you.
A creative solution would be to pass the number to a text-to-speech program, and then pipe the resulting audio to a speech-to-text program configured to use words for numbers.
Done:
just add this to the code: it is actually text to speech so it is a half baked idea .. Lololo
Code:
if [[ ${#chrlen} == 1 ]] ;then
printf "single_digits[$number - 0]\n"
printf ""${single_digits[$number]}"\n"
espeak "${single_digits[$number]}"
fi
(1) "Pick a programming language ... any(!) programming language!" (I can truthfully say that I solved this same problem thirty-five years ago in HP2000 Access BASIC.)
(2) The essence of this problem is nothing more than a very-trivial exercise in integer arithmetic ... the integer-division and integer-modulous operators. Once you have succeeded in breaking down "135" into "1," "3," and "5," you're home free.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.