Strings to numbers

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

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

#16 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:23 PM

View PostCanis, on 02 August 2013 - 06:18 PM, said:

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!



wait ignore this.. i just realized how stupid that for loop is .. it will match even if its just one letter
sorry, will come up with another code, however, can you still answer the question above? that how will i compare whats after "twenty" in the "twentyone"

thanks
Was This Post Helpful? 0
  • +
  • -

#17 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3472
  • View blog
  • Posts: 10,706
  • Joined: 05-May 12

Re: Strings to numbers

Posted 02 August 2013 - 06:36 PM

Have you covered pointers yet in class?
Was This Post Helpful? 1
  • +
  • -

#18 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:43 PM

View PostSkydiver, on 02 August 2013 - 06:36 PM, said:

Have you covered pointers yet in class?

yep, am aware of pointers.. but its not my strong suit
Was This Post Helpful? 0
  • +
  • -

#19 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3472
  • View blog
  • Posts: 10,706
  • Joined: 05-May 12

Re: Strings to numbers

Posted 02 August 2013 - 06:56 PM

If you found a match for N characters, then you advance your pointer into the string N characters so that you skip over the ones that you already matched.
Was This Post Helpful? 1
  • +
  • -

#20 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,225
  • Joined: 20-March 10

Re: Strings to numbers

Posted 03 August 2013 - 03:35 PM

Hmm, yeah you could do it that way.

Or you could do it this way.

I have implemented everything but 11-19, which I have deliberately left out

but that is easily implemented in this code pretty much in the same way as units and tens

it should give you some ideas on how to implement your own version though.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int completer(char* lex, char* input, int slength)
{
    char* complete= malloc(sizeof(char)*slength);




    int i =0;
    for(i=0; i<slength; i++)
    {
        complete[i]= input[i];
    }

    if(strncmp(lex,complete,slength)==0)
    {
        free(complete);
        return 1;
    }
    free(complete);
    return 0;
}

int parse(char* input, char* chart[10],int multiplier)
{
    int j=0,i=0,flag=0,result=-1;

    size_t slength;
    for(i=0; i<10; i++)
    {

        slength= strlen(chart[i]);
        for(j=0; j<slength; j++)
        {
            if(chart[i][j]==input[j]);
            {
                if(completer(chart[i], input, slength)==1)
                {
                    flag =1;
                }
                if(flag==1)
                    break;
            }
            if(flag==1)
                break;
        }
        if(flag==1)
            break;
    }

    if(flag==1)
    {

        if(multiplier==1)
        {
            result =i*multiplier;
        }
        else
        {
            result = (i+1)*multiplier;
        }




    }

    return result;
}

int main()
{
    char* units[10]= {"zero","one","two","three","four","five","six","seven","eight","nine"};
    char* tens[10]= {"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety","hundred"};


    int i=0,n=0;
    char input[20] ="twentytwo";
    char *input2= calloc(strlen(input),sizeof(char));
    i=parse(input,tens,10);

    if(i>0)
    {

        int length = strlen(tens[(i/10)-1]);
        for(n=strlen(tens[(i/10)-1]); n<strlen(input); n++)
        {
            input2[n-length] = input[n];

        }

    }
    else
    {
        strcat(input2,input);
    }


    n=parse(input2,units,1);


    if(i!=-1&&n!=-1)
    {
        printf("%s expressed as a number is %d", input, i+n);
    }
    else if(n!=-1)
    {
        printf("%s expressed as a number is %d", input, n);
    }
    else if(i!=-1)
    {
        printf("%s expressed as a number is %d", input, i);
    }
    else
        printf("Error !");
    free(input2);
    return 0;
}



Was This Post Helpful? 0
  • +
  • -

#21 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,027
  • Joined: 08-August 08

Re: Strings to numbers

Posted 03 August 2013 - 06:05 PM

That looks a little hard to follow. Using C++ it's easier to see the overall logic:
#include <iostream>
#include <string>

using namespace std;

int num(string str)
{
	const string ones[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
	const string tens[10] = {"","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
	const string teens[10] = {"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eightteen","nineteen"};
	int i, number = 0;
	long position;

	// Check for 10 through 19
	for (i = 0; i < 10; i++) {
		
		position = str.find(teens[i]);
		if (position > -1) {
			number = i+10;
			break;
		}
	
	}
	
	// If number == 0 and str isn't "zero" then nothing was found above, so keep looking.
	if (number == 0 && str != ones[0]) {
		
		// Check for tens digits (20, 30, ... 80, 90)
		for (i = 2; i < 10; i++) {
			position = str.find(tens[i]);
			if (position > -1) {
				number = i * 10;
				str = str.substr(position+tens[i].length());
				break;;
			}
		}
		
		// Check for ones digits
		for (i = 0; i < 10; i++) {
			position = str.find(ones[i]);
			if (position > -1) {
				number += i;
				break;
			}
		}

	}
	
	return number;
}

int main(int argc, const char * argv[])
{

	string test[6]={"thirtyone", "fifty","fourteen","ninetyeight","zero","three"};
	int thevalue;
	
	for (int tst = 0; tst < 6; tst++) {
		thevalue = num(test[tst]);
		cout << thevalue << endl;
	}
	
	return 0;
}

and it's still up to the OP to write a C version. ;)
Was This Post Helpful? 1
  • +
  • -

#22 Canis  Icon User is offline

  • D.I.C Head

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

Re: Strings to numbers

Posted 03 August 2013 - 06:38 PM

View PostCTphpnwb, on 03 August 2013 - 06:05 PM, said:

That looks a little hard to follow. Using C++ it's easier to see the overall logic:
#include <iostream>
#include <string>

using namespace std;

int num(string str)
{
	const string ones[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
	const string tens[10] = {"","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
	const string teens[10] = {"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eightteen","nineteen"};
	int i, number = 0;
	long position;

	// Check for 10 through 19
	for (i = 0; i < 10; i++) {
		
		position = str.find(teens[i]);
		if (position > -1) {
			number = i+10;
			break;
		}
	
	}
	
	// If number == 0 and str isn't "zero" then nothing was found above, so keep looking.
	if (number == 0 && str != ones[0]) {
		
		// Check for tens digits (20, 30, ... 80, 90)
		for (i = 2; i < 10; i++) {
			position = str.find(tens[i]);
			if (position > -1) {
				number = i * 10;
				str = str.substr(position+tens[i].length());
				break;;
			}
		}
		
		// Check for ones digits
		for (i = 0; i < 10; i++) {
			position = str.find(ones[i]);
			if (position > -1) {
				number += i;
				break;
			}
		}

	}
	
	return number;
}

int main(int argc, const char * argv[])
{

	string test[6]={"thirtyone", "fifty","fourteen","ninetyeight","zero","three"};
	int thevalue;
	
	for (int tst = 0; tst < 6; tst++) {
		thevalue = num(test[tst]);
		cout << thevalue << endl;
	}
	
	return 0;
}

and it's still up to the OP to write a C version. ;)/>


that is actually almost the thing i have already written, the problem is that it will record the tens "20, 30.. etc" but it wont record the ones after it, so "twentyone" will only be "twenty" not "twentyone"
Was This Post Helpful? 0
  • +
  • -

#23 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: Strings to numbers

Posted 03 August 2013 - 06:58 PM

You will need to create a dictionary of "words" that make up the currency. For example, one, two, ..., ten, twenty, thirty, ... ninety. As already shown. Then when you search for a match you first look for the "tens" and if you find one of these values you will then search for the "ones".

For example thirtyfour. You check thru your dictionary and find that the "tens" place is thirty. Now search the string for the "ones" and you find four. Thirty plus four is equal to 34.

Jim

This post has been edited by jimblumberg: 03 August 2013 - 06:59 PM

Was This Post Helpful? 1
  • +
  • -

#24 Canis  Icon User is offline

  • D.I.C Head

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

Re: Strings to numbers

Posted 03 August 2013 - 06:59 PM

position = str.find(teens[i]);
position = str.find(tens[i]);
position = str.find(ones[i]);


what do those lines do exactly?
Was This Post Helpful? 0
  • +
  • -

#25 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,027
  • Joined: 08-August 08

Re: Strings to numbers

Posted 03 August 2013 - 07:13 PM

They return the position found for the beginning of the word in the array (teens, tens, or ones). If the word isn't found they return -1.
Did you try the code? The first test word is "thirtyone" and the output is 31.
Was This Post Helpful? 1
  • +
  • -

#26 Canis  Icon User is offline

  • D.I.C Head

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

Re: Strings to numbers

Posted 03 August 2013 - 07:57 PM

View PostCTphpnwb, on 03 August 2013 - 07:13 PM, said:

They return the position found for the beginning of the word in the array (teens, tens, or ones). If the word isn't found they return -1.
Did you try the code? The first test word is "thirtyone" and the output is 31.


i have no experience in C++ just C, and by just what i know your code looked so much alike to mine, but the only thing different was the position thing, so i see you used str.find or something, is there something in C that can do that? to return the position ? I will post my code tomorrow that i made and maybe then you can tell me whats wrong with it, but the only thing stopping me is that most of my classmates love to just 'google' the question and then copy paste and change all the variable names.. i dont want that
Was This Post Helpful? 0
  • +
  • -

#27 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,027
  • Joined: 08-August 08

Re: Strings to numbers

Posted 03 August 2013 - 08:09 PM

C strings are arrays of characters, but C++ strings are objects that have their own methods. You'll have to write your own find.
Was This Post Helpful? 1
  • +
  • -

#28 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: Strings to numbers

Posted 03 August 2013 - 08:31 PM

Think about something like strtok() to replace the string.find() functionality.

You really need to post your code. Your code will help show us things like your level of experience, how you're trying to accomplish the objectives, etc. Since there are usually many ways of accomplishing the same task, the code helps illustrate your though process, making it much easier for us to help you.


Jim
Was This Post Helpful? 1
  • +
  • -

#29 Canis  Icon User is offline

  • D.I.C Head

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

Re: Strings to numbers

Posted 03 August 2013 - 08:52 PM

View Postjimblumberg, on 03 August 2013 - 08:31 PM, said:

Think about something like strtok() to replace the string.find() functionality.

You really need to post your code. Your code will help show us things like your level of experience, how you're trying to accomplish the objectives, etc. Since there are usually many ways of accomplishing the same task, the code helps illustrate your though process, making it much easier for us to help you.


Jim

ok here goes,
char single[10][10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
char teen[10][10] = {"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
char dubs[10][10] = {"zero","zero","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
char trips[3][10] = {"hundred", "thousand","million"};

int convert(char str[20]){
	int converted=0;
	for(int i=0;i<10;i++){
		if(strcmp(str,single[i])==0){
			converted+=i;
		}
		
		if(strcmp(str,teen[i])==0){
				converted+=(i+10);
			}
		if(strcmp(str,dubs[i])==0){
			converted+=(i*10);
		}
	}
return converted;
}


The input is a text file, so in the main it takes care of reading the file line by line and then printing it,, but heres the actual function to change it

It works for every number except when there is a case like "twentyeight" then it doesnt work

This post has been edited by Canis: 03 August 2013 - 08:54 PM

Was This Post Helpful? 0
  • +
  • -

#30 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,027
  • Joined: 08-August 08

Re: Strings to numbers

Posted 03 August 2013 - 09:10 PM

You're checking single, then teen, then dubs. My code checks teens, then the tens digits, then ones. The order matters:
twenyone starts with the tens digit "twenty."
Was This Post Helpful? 1
  • +
  • -

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