Strings to numbers

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 1379 Views - Last Post: 04 August 2013 - 12:34 PM Rate Topic: -----

#1 Canis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 01-February 13

Strings to numbers

Posted 01 August 2013 - 09:31 PM

Hey everyone,

So i need to read from a text file and in the text file there are going to be lines like "twentyfour" and "eleven" and "seventynine", you get the idea; and what i need to do is to convert that, into the actual number, so 'twentyfour' will be 24. and then store into an array and i can go from there.. i do have an idea how to do this, but im afraid it will be long and messy, can anyone give an opinion on how they would tackle this? to make a function that will read the string "twentyfour" and then convert it to 24.. just looking for programming logic or something

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Strings to numbers

#2 nandureddy  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 129
  • Joined: 31-January 11

Re: Strings to numbers

Posted 01 August 2013 - 10:06 PM

Refer to this words-to-numbers-conversion on Dream.In.Code.

This post has been edited by nandureddy: 01 August 2013 - 10:07 PM

Was This Post Helpful? 1
  • +
  • -

#3 Canis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 01-February 13

Re: Strings to numbers

Posted 01 August 2013 - 11:16 PM

Hey thanks for your answer. Well that was my initial thought, the way he did it.. but i dont know how that would work in C because how would i be able to define that 1 : one and 2 : two etc. So is there no other way than to do that ?
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 834
  • View blog
  • Posts: 2,457
  • Joined: 20-March 10

Re: Strings to numbers

Posted 02 August 2013 - 12:56 AM

Is there a limit on the numbers ?

That is for example the numbers only go to
100.

If this is C I would probably use a struct with
a two dimensional char array.

Then match the words function that returns the
position where the word is found.

zero at position 0

seventynine at positon 79

This is a pretty brute force way of doing it though

You could alternatively have a parser parse each
Sentence and break it into tens and units
That would require two arrays in your struct
One for tens one for units then add them to
calculate the total
If you needed hundreds that would require 3
Arrays thousands four arrays and so on...

Regards

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3070
  • View blog
  • Posts: 10,765
  • Joined: 08-August 08

Re: Strings to numbers

Posted 02 August 2013 - 07:38 AM

Note that even if you get this working it may be that your text file may not follow the rules as accurately as you might hope. For example, it might have "one hundred twenty-five", "one hundred twentyfive", "onehundredtwentyfive", or even "one hundred and twenty five", or some other combination of spaces, hyphens, and the the word "and."
Was This Post Helpful? 1
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 834
  • View blog
  • Posts: 2,457
  • Joined: 20-March 10

Re: Strings to numbers

Posted 02 August 2013 - 07:57 AM

View PostCTphpnwb, on 02 August 2013 - 02:38 PM, said:

Note that even if you get this working it may be that your text file may not follow the rules as accurately as you might hope. For example, it might have "one hundred twenty-five", "one hundred twentyfive", "onehundredtwentyfive", or even "one hundred and twenty five", or some other combination of spaces, hyphens, and the the word "and."

Well,

You would hope there would be some standardisation of rules as then it becomes a major headache to parse.

But you raise a good point in that we dont currently know what those rules are.

Can the OP provide them ?

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#7 Canis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 01-February 13

Re: Strings to numbers

Posted 02 August 2013 - 12:36 PM

Well the rules go that this should work for any realistic number, so one thousand twenty five is fine, and the standardization for it is that all the numbers in the text file will be written without any spaces or hyphens, so onethousandtwentyfive or twohundredeightyseven .. etc
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1396
  • View blog
  • Posts: 4,859
  • Joined: 19-February 09

Re: Strings to numbers

Posted 02 August 2013 - 12:55 PM

If you have an array of string for the units say "one", "two" etc.. You can compare each unit against the number word.

So "one", which is three characters, is compared against the first three characters of "onethousandtwentyfive" so you have a starting value of 1.
Was This Post Helpful? 1
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3070
  • View blog
  • Posts: 10,765
  • Joined: 08-August 08

Re: Strings to numbers

Posted 02 August 2013 - 01:10 PM

View PostCanis, on 02 August 2013 - 03:36 PM, said:

..so onethousandtwentyfive or twohundredeightyseven .. etc

Even your example follows two different rules. To be consistent you would say:

onethousandtwentyfive
twohundredandeightyseven

or

onethoustwentyfive
twohundredeightyseven

Do you have the text file, or do you need to create it on your own?
Was This Post Helpful? 1
  • +
  • -

#10 Canis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 01-February 13

Re: Strings to numbers

Posted 02 August 2013 - 02:52 PM

I have a text file given as an example, here is what is in it:

seventyeight
twentyeight
thirty
fortytwo
thirty
fiftysix
fiftyfour
seventyfour
sixtyfour
twenty
fiftysix
thirty
twentysix
twenty
fortyfour
fortyfour
fiftysix
fiftyfour
seventyfour
sixtyfour
fiftytwo
thirtyeight
thirtytwo
twentyfour
twentysix
sixty

so i guess in this example there are no hundreds, but lets just keep it so the input text file will be onetwentyseven or twofiftyone or threesixtysix etc

This post has been edited by Canis: 02 August 2013 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,415
  • Joined: 05-May 12

Re: Strings to numbers

Posted 02 August 2013 - 02:58 PM

Interesting... That means that your string parsing needs to be greedy so that you can parse "six", "sixty" and "sixtysix" correctly.
Was This Post Helpful? 1
  • +
  • -

#12 Canis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 01-February 13

Re: Strings to numbers

Posted 02 August 2013 - 03:51 PM

View PostSkydiver, on 02 August 2013 - 02:58 PM, said:

Interesting... That means that your string parsing needs to be greedy so that you can parse "six", "sixty" and "sixtysix" correctly.

Yeah

Well i wrote up some code write now and ran into a little problem, I am using strcmp where the first str is the input text and the other string is the predefined number as "twenty" "fourty" "two" etc. And then if they match, it stores it into an array (temporarily, to test code). So the problem is that, when i strcmp, it will only be matching if the input text is "seventy" so it matches with it.. but what if the input is "seventytwo" then it doesnt match.. How do you proceed to fix this? are there any alternatives? that i can compare the first part of input and then second part.

I would post my code here but the thing is that i know a lot of my classmates like to google and copy paste code, so i will not post it here for now
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,415
  • Joined: 05-May 12

Re: Strings to numbers

Posted 02 August 2013 - 04:08 PM

Strncmp is your friend.

Sent from my T-Mobile G2 using Tapatalk 2
Was This Post Helpful? 1
  • +
  • -

#14 Canis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 01-February 13

Re: Strings to numbers

Posted 02 August 2013 - 04:32 PM

View PostSkydiver, on 02 August 2013 - 04:08 PM, said:

Strncmp is your friend.

Sent from my T-Mobile G2 using Tapatalk 2

Thanks, just saw what that does and that maskes sense
Was This Post Helpful? 0
  • +
  • -

#15 Canis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 01-February 13

Re: Strings to numbers

Posted 02 August 2013 - 06:18 PM

hey everyone

so i think im close, but heres the problem now; as mentioned earlier i was going to use strncmp to compare the first part of the string so .. the "twenty" of "twentyone" but i dont know, how i can compare the rest of it, so like the "one" part of it. What i did was something like this:
defined[10][10] = {"twenty","thirty"..."ninety"};
defined2[10][10] = {"one","two"..."nine"};
int finalnumber;
for(int a=0;a<10;a++){
if(strncmp(input,defined[1],a){                                //this is just pseudo, what i mean is that when
finalnumber+=10;                       //it finds a match, it will add that much value to the final number im returning



so after i found a match for the first part.. how do i get to the second part of the number ?

Thanks!

This post has been edited by Canis: 02 August 2013 - 06:19 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3