Hi,
How to write my own version of function to convert strings to numbers ? Any ideas? Just a few would be ok. Thnx in advance
Hi,
How to write my own version of function to convert strings to numbers ? Any ideas? Just a few would be ok. Thnx in advance
homework???
how about you USE YOUR HEAD??? try and do it yourself first. then ask questions when you get stuck
Quidquid latine dictum sit, altum sonatur.
Whatever is said in Latin sounds profound.
ok, now being rude is now accepted in this forum huh?
Converting strings to numbers is called hashing... here's a little hash function....Although I'm a little uncertain... this isn't about sorting strings is it?Code:int hash (const char * s) { int total = 0; for (; *s != 0; s++) { total = (*s + total * 31) % 100; } return total; }
Last edited by QuestionC; 01-22-2002 at 12:47 AM.
Callou collei we'll code the way
Of prime numbers and pings!
As you can see from the replies, a little bit more info, maybe an example, and some sign you tried to solve this yourself would help your question to get answered.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
ok here's my current code, but it doesn't work:
no output
thnxCode:#include <stdio.h> int str_to_int( char *s ); int main() { char string[] = "5550"; if ( str_to_int( string ) != 1 ) printf( "%d\n\n", str_to_int( string ) - 1 ); /* output should be 5549 */ system( "PAUSE" ); return 0; } int str_to_int( char *s ) { int length = strlen( s ); int asciiValue; int num; int total = 0; int num_of_rotation = 0; while ( length != 0 ) { asciiValue = s[ length - 1 ]; if ( ( asciiValue - 48 ) >= 0 && ( asciiValue - 48 ) < 9 ) { num = asciiValue - 48; total += num * ( 10 * num_of_rotation ); num_of_rotation++; --length; } else return 1; } return total; }
Code:int str_to_int( char *s, int *n); int main(int argc, char *argv[]) { char string[10]; int i; if(argc==2 && strlen(argv[1])<10) strcpy(string,argv[1]); else { printf("Enter a string( <10digit):"); scanf("%s8.8",string); } if(!str_to_int(string,&i)) printf("%d\n",i); else printf("char is not in 0 - 9\n"); } int str_to_int( char *s, int *n) { int length; int num; int total = 0; int strength=0; int i; length = strlen( s ); length--; for(; 0<=length; length--) { num = s[ length ] - '0'; if(num >= 0 && num <= 9) { if(strength != 0) strength*=10; else strength=1; num = num *strength; total+=num; } else return 1; } *n=total; return 0; }
[ Never code before desk work ]
-------------------------------------:-->
A man who fears Nothing is the man who Loves Nothing
If you Love Nothing, what joy is there in your life.
=------------------------------------------------------= - I may be wrong.
I'm sure this is not the thing he wanted to know..Originally posted by QuestionC
Converting strings to numbers is called hashing... here's a little hash function....Although I'm a little uncertain... this isn't about sorting strings is it?Code:int hash (const char * s) { int total = 0; for (; *s != 0; s++) { total = (*s + total * 31) % 100; } return total; }
As I know.. Hashing is used for Hash Table. which is one of storing style.. which is very efficient to search data items from the table. Time complexity is O(1).
Interesting topic.. can we discuss about it more.. ?
[ Never code before desk work ]
-------------------------------------:-->
A man who fears Nothing is the man who Loves Nothing
If you Love Nothing, what joy is there in your life.
=------------------------------------------------------= - I may be wrong.
Oh geez.... I was really off that time.
The big bug in your code appears to be pretty much this line...Code:total += num * ( 10 * num_of_rotation );Following your code, the result then hould beCode:Table showing the states of your loop... length num num_of_rotation 4 0 0 3 5 1 2 5 2 1 5 3
0 * 10 * 0 + 5 * 1 * 10 + 5 * 2 * 10 + 5 * 3 * 10 = 300
Callou collei we'll code the way
Of prime numbers and pings!
Takes no account for errors, however, that isn't what you asked forCode:int atoi(char *s) { int i, n; n = 0; for(i = 0; s[i] >= '0' && s[i] <= '9'; i++) n = 10 * n + (s[i] - '0'); return n; }
Good coding... master5001
I was trying to remember that there is a way little more simple...
Last edited by zahid; 01-22-2002 at 04:30 AM.