converting string to integers

This is a discussion on converting string to integers within the C Programming forums, part of the General Programming Boards category; Hello i'm trying to build this bit of code: i get an input (for example: "1 2 45 4 67 ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    9

    converting string to integers

    Hello
    i'm trying to build this bit of code:
    i get an input (for example: "1 2 45 4 67 3") as string
    and i need to put these numbers into an array,
    if you can help me out, i'll be grateful.
    thank you very much

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    It would be possible with strtok and strtol.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    9

    i'm not allowed

    i'm not allowed to use:
    pointers, functions, etc.
    i can use strlen,strcpy,strcmp

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Well, that just makes it more difficult, but the idea is the same.
    You would have to walk through the string to find the spaces and either truncate the string (may be a little over course) or copy it into a new array with strcpy.
    Then you would simply have to walk through that new string and translate each char into the appropriate integer and apply a little math.
    Last edited by Elysia; 06-20-2008 at 04:39 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You will need to use (either your own or a standard function [1]) that converts from string to integer, no matter what you do.

    As Elysia indicated, you would have to walk through the string and find the separator, then find the next separator (or end of string), and copy the portion of string out, then convert that string to an integer.


    [1] Ok, so we don't HAVE to make it a function - it could be code written inline in the main function - but that would not be good coding style, in my opinion - but it would remove the need to use a function if that's the requirement you have.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    9

    thank you

    unfortuantley, it's an assigment for Uni., so i don't decide what i can use,
    but never mind, i managed to do it anyways.... thank you for your help.

  7. #7
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Smile

    Maybe something like this...

    Code:
    const wchar_t *input = L"1 2 45 4 67 3";
    
    int *buffer = NULL; //the result saved here
    int count = 0; //length of the buffer
    
    
    if((buffer = malloc(sizeof(int))) == NULL) //actually, statements like this could be
       exit(EXIT_FAILURE);                     //defined as a macro if you wish...
    
    size_t i;
    
    wchar_t *temp = NULL; //c-style string to save each number
    size_t j = 0; //strlen(temp) == j
    
    for(i=0; i<(wcslen(input) + 1);i++)
    {
       if(temp == NULL)
          if((temp = malloc(sizeof(char))) == NULL)
             exit(EXIT_FAILURE);
    
       //the delimiter is an unicode non-break-space ' '
       if(input[i] == L' ' || i == wcslen(input))
       {
          temp[j] = L'\0'; //c-style string, remember?
    
          buffer[count] = _wtoi(temp); //convert temp to int
          count++;
    
          if((buffer = realloc(buffer, (count + 1) * sizeof(int))) == NULL)
             exit(EXIT_FAILURE);
    
          j = 0;
          free(temp);
          temp = NULL;
    
          continue;
       }
    
       temp[j] = input[i];
       j++;
    
       if((temp = realloc(temp, (j + 1) * sizeof(wchar_t))) == NULL)
          exit(EXIT_FAILURE);
    }
    
    for(i=0; i<count; i++)
    {
       //print out the result:
       printf("%d\n", buffer[i]);
    }
    
    free(buffer); //clean-up
    
    //happy ending...
    I think there is a simple algorithm using strtok... but I still don't understand how to use it...

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you use wchar_t *temp, then your malloc should also use
    wchar_t here:
    Code:
    if((temp = malloc(sizeof(char))) == NULL)
             exit(EXIT_FAILURE);
    Moreover, you are only ever allocating one byte, but storing potentially more in the string.

    And temp is never freed, so it's a memory leak too.

    I would suggest using a fixed size string for this purpose - a number that is longer than (say) 15 digits will not be a valid integer [unless it has redundant leading zeros of course], so there's no need to have a string bigger than 16 chars. Saves all sort of problems with allocating memory - but make sure that you detect longer strings before trying to copy.

    I would also, unless there is clear need to support a HUGE number of numbers, have buffer as a statically sized array - again, simplifying the code [and avoiding problems with what to do when allocation fails].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    The assignment was no pointers, so that little solution fails.
    But if you wanted to do it with pointers, then sure, that's one way.
    Although, unfortunately, the code is not without bugs.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #10
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    If you use wchar_t *temp, then your malloc should also use
    wchar_t here:
    Code:
    if((temp = malloc(sizeof(char))) == NULL)
             exit(EXIT_FAILURE);
    Moreover, you are only ever allocating one byte, but storing potentially more in the string.
    Sorry, the code was ANSI before. I forget that one. Thanks.
    And temp is never freed, so it's a memory leak too.
    Please take a look at...
    Code:
    if(input[i] == L' ' || i == wcslen(input))
    {
    
       //...
    
       j = 0;
       free(temp);
       temp = NULL;
    
       //...
    
    }
    At the end of the loop the temp is freed.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    But temp is always one wchar_t, and it contains '\0'.
    It never contains any other data, so trying to convert it to a number is meaningless.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    But temp is always one wchar_t, and it contains '\0'.
    I wonder, where is it?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by audinue View Post
    I wonder, where is it?
    Where is what? You need to allocate strlen(something), and not sizeof(char). And you never assign anything to temp, except \0.

    EDIT: Hang on, I see a for-loop that I missed before. (Yes, I got lost in the indentation.) So that should fill your temp array.
    Last edited by tabstop; 06-20-2008 at 10:43 AM.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Quote Originally Posted by tabstop View Post
    Where is what? You need to allocate strlen(something), and not sizeof(char). And you never assign anything to temp, except \0.
    Exactly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by audinue View Post
    Sorry, the code was ANSI before. I forget that one. Thanks.

    Please take a look at...
    Code:
    if(input[i] == L' ' || i == wcslen(input))
    {
    
       //...
    
       j = 0;
       free(temp);
       temp = NULL;
    
       //...
    
    }
    At the end of the loop the temp is freed.
    Ok, I admit I didn't see that. So if i != wcslen(input) and the string doesn't end with a space, it won't do that, right - and I'm pretty sure that i < wcslen(input) in your for-loop will prevent it from hitting that bit of code...

    Doing a realloc for every char in the string is REALLY bad coding practice. realloc itself is quite time consuming, and it will copy the content of the string each time [well, ok, so a decent implementation of realloc will just grow in place some of the time, but you don't know that this is how it's implement, nor when and under what conditons]. I still think you should use a fixed array size. Saves a lot of hazzle, and it's more efficient too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting a string of words into correct cases
    By BigFish21 in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2008, 12:53 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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