Strings to numbers

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

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

#31 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 - 09:17 PM

View PostCTphpnwb, on 03 August 2013 - 09:10 PM, said:

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."


if the order switching works, i will love u

View PostCanis, on 03 August 2013 - 09:13 PM, said:

View PostCTphpnwb, on 03 August 2013 - 09:10 PM, said:

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."


if the order switching works, i will love u


nopeee doesnt work, and i think i know why because, the strcmp function checks if all the characters are the same, it doesnt look for any matching chars in a string.. so when its comparing str to ones in my code, it wont match because twentyone!=one
Was This Post Helpful? 0
  • +
  • -

#32 jimblumberg  Icon User is offline

  • member icon


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

Re: Strings to numbers

Posted 03 August 2013 - 09:19 PM

Also you should be using strncmp() instead of strcmp().

strncmp(single[i], str, strlen(single[i]))


This function limits the search to the number of characters provided in the third argument.

Jim

This post has been edited by jimblumberg: 03 August 2013 - 09:22 PM

Was This Post Helpful? 1
  • +
  • -

#33 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 - 09:25 PM

View Postjimblumberg, on 03 August 2013 - 09:19 PM, said:

Also you should be using strncmp() instead of strcmp().

strncmp(single[i], str, strlen(single[i]))


This function limits the search to the number of characters provided in the third argument.

Jim


yeah someone mentioned strncmp, and i wrote up some code for that.. but got stuck
	for(int i=0;i<10;i++){
		if(strcmp(str,teen[i]==0){
				converted+=(i+10);
			}
		if(strncmp(str,dubs[i],5==0){
			converted+=(i*10); //5 letters will check for 'sixty'
		===== here goes the rest to see whats after 'sixty'.. but i dont know how 
		}
		if(strncmp(str,dubs[i],6==0){
			converted+=(i*10);
		}
		if(strncmp(str,dubs[i],7==0){
			converted+=(i*10);
		}
		if(strcmp(str,single[i])==0){
			converted+=i;
		}
}


Was This Post Helpful? 0
  • +
  • -

#34 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: Strings to numbers

Posted 03 August 2013 - 09:28 PM

Look at Jim's use of the function. How is it different from yours?
Was This Post Helpful? 2
  • +
  • -

#35 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 - 09:37 PM

hmm.. i might just try that, comparing it for the strlength of the "one" "two"..
Was This Post Helpful? 0
  • +
  • -

#36 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 - 10:03 PM

ahhhhhh im soo close!
		if(strncmp(dubs[i],str,strlen(dubs[i])==0){
				position=strlen(dubs[i]);
				for(int x=0;x<10;x++){
					if(strncmp(single[x],str,===== HERE =====))==0){
					converted+=x;
				}
			}
	}
return converted;
}


ok, so where it says ====HERE====, how do i write that, i want to strncmp(single[x],str,AFTER POSITION ONLY).. so how would one write, to compare, whatever is AFTER position.. i tried position + strlen(single) but that wouldnt work cuz it will check the whole thing again..

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

Was This Post Helpful? 0
  • +
  • -

#37 jimblumberg  Icon User is offline

  • member icon


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

Re: Strings to numbers

Posted 03 August 2013 - 10:16 PM

Quote

ok, so where it says ====HERE====, how do i write that, i want to strncmp(single[x],str,AFTER POSITION ONLY)..

This is where strstr() comes in useful.

You really need to start looking at the documentation for the C-string functions contained in string.h, your going to need to use several of these functions for this problem.

And unless absolutely necessary I recommend you stop trying to support hundreds. At least until you get "ones", "teens", then "tens" completed.

Jim
Was This Post Helpful? 1
  • +
  • -

#38 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1313
  • View blog
  • Posts: 4,512
  • Joined: 19-February 09

Re: Strings to numbers

Posted 03 August 2013 - 10:26 PM

View PostCanis, on 04 August 2013 - 06:03 AM, said:

ok, so where it says ====HERE====, how do i write that, i want to strncmp(single[x],str,AFTER POSITION ONLY).. so how would one write, to compare, whatever is AFTER position.. i tried position + strlen(single) but that wouldnt work cuz it will check the whole thing again..


You should be able to add the position on to the string to say start at position.

strncmp(single[x], str + position, ...)


Was This Post Helpful? 2
  • +
  • -

#39 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 - 10:48 PM

View Post#define, on 03 August 2013 - 10:26 PM, said:

View PostCanis, on 04 August 2013 - 06:03 AM, said:

ok, so where it says ====HERE====, how do i write that, i want to strncmp(single[x],str,AFTER POSITION ONLY).. so how would one write, to compare, whatever is AFTER position.. i tried position + strlen(single) but that wouldnt work cuz it will check the whole thing again..


You should be able to add the position on to the string to say start at position.

strncmp(single[x], str + position, ...)


this worked!!
Was This Post Helpful? 0
  • +
  • -

#40 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 - 10:55 PM

I thank you all for everything! The hundreds and thousand after will be quite easy now that i have the idea of it, Thank you again! you guys were quite very helpful! gotta say you know your programming!
Was This Post Helpful? 0
  • +
  • -

#41 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,742
  • Joined: 05-May 12

Re: Strings to numbers

Posted 04 August 2013 - 10:29 AM

Since the OP has solved the problem, I'm going to post how I would have tackled the problem. The code below is deliberately incomplete just in case he isn't truly done yet, or any of his classmates stumble upon this thread.
#define CountOf(_a) (sizeof(_a)/sizeof(_a[0]))

typedef struct
{
    char * pszWord;
    int nValue;
    int nMultiplier;
} WordValue;

WordValue g_mpWordValue[] =
{
    { "negative", 0, -1 },
    { "thousand", 0, 1000 },
    { "hundred", 0, 100 },
    :
    { "sixty", 60, 1 },
    :
    { "sixteen", 16, 1 },
    :
    { "six", 6, 1 },
    :
    { "zero", 0, 1 },
};

WordValue * FindWordValue(char * psz)
{
    WordValue * pwordvalue = &g_mpWordValue[0];
    WordValue * pwordvalueEnd = &g_mpWordValue[CountOf(g_mpWordValue)]; 
    while (pwordvalue != pwordvalueEnd)
    {
        if (!strncmp(psz, pwordvalue->word, strlen(pwordvalue->word))
            return pwordvalue;
    }
    return NULL;
}

int WordsIntoValue(char *psz)
{
    int nValue = 0;

    while (*psz)
    {
        WordValue * pwordvalue = FindWordValue(psz);

        if (pwordvalue == NULL)
            return INT_MIN;

        nValue *= pwordvalue->multiplier;
        nValue += pwordvalue->value;
        psz += strlen(pwordvalue->word);
    }
    return nValue;
}


Was This Post Helpful? 0
  • +
  • -

#42 Canis  Icon User is offline

  • D.I.C Head

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

Re: Strings to numbers

Posted 04 August 2013 - 12:34 PM

Thanks skydiver, but we were told not to involve any structs yet
Also I will post the full solution if any would like me to, after today because te due date is today
Also thanks for the help again! Learnt some new things
Was This Post Helpful? 0
  • +
  • -

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