My own version of function to convert strings to numbers ?

This is a discussion on My own version of function to convert strings to numbers ? within the C Programming forums, part of the General Programming Boards category; Hi, How to write my own version of function to convert strings to numbers ? Any ideas? Just a few ...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    My own version of function to convert strings to numbers ?

    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

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    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.

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Are you Nutshell's mother?

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    ok, now being rude is now accepted in this forum huh?

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    You can convert any character to an integer using a cast.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Converting strings to numbers is called hashing... here's a little hash function....
    Code:
    int hash (const char * s)
    {
     int total = 0;
     for (; *s != 0; s++)
     {
      total = (*s + total * 31) % 100;
     }
     return total;
    }
    Although I'm a little uncertain... this isn't about sorting strings is it?
    Last edited by QuestionC; 01-21-2002 at 11:47 PM.
    Callou collei we'll code the way
    Of prime numbers and pings!

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    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.

  8. #8
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    ok here's my current code, but it doesn't work:
    no output

    Code:
    #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;
    }
    thnx

  9. #9
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    532

    Check if it works...

    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.

  10. #10
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    532
    Originally posted by QuestionC
    Converting strings to numbers is called hashing... here's a little hash function....
    Code:
    int hash (const char * s)
    {
     int total = 0;
     for (; *s != 0; s++)
     {
      total = (*s + total * 31) % 100;
     }
     return total;
    }
    Although I'm a little uncertain... this isn't about sorting strings is it?
    I'm sure this is not the thing he wanted to know..

    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.

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    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 );
    Code:
    Table showing the states of your loop...
    length  num  num_of_rotation
         4   0              0
         3   5              1
         2   5              2
         1   5              3
    Following your code, the result then hould be
    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!

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    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;
    }
    Takes no account for errors, however, that isn't what you asked for

  13. #13
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    532
    Good coding... master5001
    I was trying to remember that there is a way little more simple...
    Last edited by zahid; 01-22-2002 at 03:30 AM.

  14. #14
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    wow how short is your code? Masterpiece...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21