# Words to numbers conversion

Page 1 of 1

## 11 Replies - 9107 Views - Last Post: 30 December 2010 - 02:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=207487&amp;s=cce7f1c5411410b70c609de6ad367b1a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hazeleyes

Reputation: 0
• Posts: 6
• Joined: 29-December 10

# Words to numbers conversion

Posted 29 December 2010 - 10:05 AM

Hi,
can any one guide me? how can i convert string to number conversion.for example. if i want to convert
onethousandeightynine to 1089 without using delimiter like one-thousand-eighty-five
etc
Is This A Good Question/Topic? 0

## Replies To: Words to numbers conversion

### #2 CodeGrappler

Reputation: 41
• Posts: 120
• Joined: 29-November 10

## Re: Words to numbers conversion

Posted 29 December 2010 - 10:14 AM

Please post some code showing us that you have tried to solve this and we will help guide you.

Definitely sounds like a school assignment.

### #3 hazeleyes

Reputation: 0
• Posts: 6
• Joined: 29-December 10

## Re: Words to numbers conversion

Posted 29 December 2010 - 10:20 AM

```int main()
{
//char lib[][]={"one","two","three","four","five","six","seven"};
string str1[10]={"Zero","One","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine "};
string str2[10]={"Ten ","Eleven ","Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen "};
string data= "onehundredthirtyfour";
char * cdata=data.c_str();
char * ptr;
char out[15]={'\0'};
for(int i=0;i<=data.length();i++)
{
for (int j=0 ;j <= 11 ; j++)
{
```

I have studied C++ few years back and now i m trying to solve this question asked for preparation of a job interview and it is definitely not a school task.

just guide me.. suppose i have a string "onehundredthirtyfour" .. I am struck here,becz there is no delimiter like '-'.. should i use some pointer. first i need to convert this string to character string?

MOD EDIT: When posting code...USE CODE TAGS!!!

This post has been edited by JackOfAllTrades: 29 December 2010 - 11:23 AM

### #4 KYA

• yay verily

Reputation: 3155
• Posts: 19,200
• Joined: 14-September 07

## Re: Words to numbers conversion

Posted 29 December 2010 - 10:25 AM

This task doesn't make any sense. Reading in characters until you hit a known token is the only way to do it without having a delimiter.

I'll write a brief example, is this in C or C++? There's no reason to willingly mix char* and char[] with std::string.

### #5 hazeleyes

Reputation: 0
• Posts: 6
• Joined: 29-December 10

## Re: Words to numbers conversion

Posted 29 December 2010 - 10:28 AM

C++

### #6 mojo666

Reputation: 383
• Posts: 832
• Joined: 27-June 09

## Re: Words to numbers conversion

Posted 29 December 2010 - 10:33 AM

It seems like the first thing you should do is write a program that splits the string by word. Make an array of all possible words (one, ten, *teen, *ty, hundred, thousand, ect) then search your input string for those words and add delimiters before and after each occurance. Until this is done, you shouldn't worry about translation.

### #7 KYA

• yay verily

Reputation: 3155
• Posts: 19,200
• Joined: 14-September 07

## Re: Words to numbers conversion

Posted 29 December 2010 - 10:43 AM

Here's a quick proof of concept:

```#include <iostream>
#include <string>
#include <map>
using namespace std;

//globals for a quick proof of concept
map<string, int> reference;
//zero allows more readable index access, i.e. one is ones[1] rather then ones[0]
string ones[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};

void storeOnes(){
for(int i = 0; i < 11; i++){
reference[ones[i]] = i;
}
}

int main(){
//set up
storeOnes();
string test = "onetwothreetwofour";
string buffer;

for(int i = 0; i < test.length(); i++){
buffer.push_back(test.at(i));
map<string, int>::iterator it = reference.find(buffer);
if(it != reference.end()){
cout << (*it).second;
buffer = "";
}
}
cout << endl << endl;
system("pause");
return 0;
}

```

### #8 hazeleyes

Reputation: 0
• Posts: 6
• Joined: 29-December 10

## Re: Words to numbers conversion

Posted 29 December 2010 - 10:45 AM

Thanks.

### #9 KYA

• yay verily

Reputation: 3155
• Posts: 19,200
• Joined: 14-September 07

## Re: Words to numbers conversion

Posted 29 December 2010 - 10:50 AM

Does need some modification though, for example, this approach will quit too early on cases like fourty.

### #10 hazeleyes

Reputation: 0
• Posts: 6
• Joined: 29-December 10

## Re: Words to numbers conversion

Posted 29 December 2010 - 03:25 PM

KYA, on 29 December 2010 - 09:50 AM, said:

Does need some modification though, for example, this approach will quit too early on cases like fourty.

in case of four <=> fourty , eight <=>eighty ..one way would be to push these in buffer in reverse order..
so that ytruof "40" will be different from rouf "4" . but then how would i use this to find.because now in buffer data is in reverse order.
map<string, int>::iterator it = reference.find(buffer);
One way would be to again call some function to reverse buffer before used in .find(); but that would be not good because reversing buffer for each new character will be very memory consuming.
Any suggestions?
thanks

### #11 hazeleyes

Reputation: 0
• Posts: 6
• Joined: 29-December 10

## Re: Words to numbers conversion

Posted 29 December 2010 - 05:15 PM

Ah.... i have done..

now the only problem is... for example.. for 2203 . if input is twentytwohundredthree then its working.
but if input is twothousandtwohundredthree then its not. because in code i check if thousand or hundred comes into string i simply multiply answer with that..

Thanks you all guys,

### #12 mojo666

Reputation: 383
• Posts: 832
• Joined: 27-June 09

## Re: Words to numbers conversion

Posted 30 December 2010 - 02:35 PM

You should only multiply by the max. If a number contains "thousand" and "hundred", then you only multiply by 1000 since thousand is bigger than hundred. This does not hold if you try to account for improper numbers (ie. some people mights say onethousandthousand instead of onemillion).