11 Replies - 5138 Views - Last Post: 30 December 2010 - 02:35 PM Rate Topic: -----

#1 hazeleyes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 hazeleyes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!!!

:code:

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

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 hazeleyes  Icon User is offline

  • New D.I.C Head

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

Re: Words to numbers conversion

Posted 29 December 2010 - 10:28 AM

C++
Was This Post Helpful? 0
  • +
  • -

#6 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 337
  • View blog
  • Posts: 730
  • 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.
Was This Post Helpful? 1
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • 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;
}


Was This Post Helpful? 2
  • +
  • -

#8 hazeleyes  Icon User is offline

  • New D.I.C Head

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

Re: Words to numbers conversion

Posted 29 December 2010 - 10:45 AM

Thanks.
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 hazeleyes  Icon User is offline

  • New D.I.C Head

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

Re: Words to numbers conversion

Posted 29 December 2010 - 03:25 PM

View PostKYA, 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
Was This Post Helpful? 0
  • +
  • -

#11 hazeleyes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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,
Was This Post Helpful? 0
  • +
  • -

#12 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 337
  • View blog
  • Posts: 730
  • 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).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1