-2

So now that I've figured out how to get what I want, I'm just hoping somebody can let me know a cleaner, less ridiculous way of achieving the same thing. I'm just learning C. Here was my approach.

int main()
{
    // String of positive and negative integer values 
    // Numbers are never more than 2 digits

    char TEMPS[256] = "1 -22 -8 14 5";
    int N = 5;

    int ints[N];
    int i = 0;
    int mult;

    // Arbitrary number to identify that num is not yet in use
    int num = 999;  

    int c = 0;
    mult = (TEMPS[c] != 45) ? -1 : 1;
    while(strcmp(&TEMPS[c], "\0") != 0)
    {  
       if(TEMPS[c] == 32)
       {            
            ints[i] = mult * num;                        
            i++;
            num = 999;
            mult = (TEMPS[c + 1] == 45) ? -1 : 1;
       } 
       else if((TEMPS[c] != 45) && (TEMPS[c] != 32))
       {
            if(num == 999)
            {
                num = TEMPS[c] - '0';
            }
            else
            {
                num = num * 10 + (TEMPS[c] - '0');
            }
       }       
       c++;
    }
    ints[i] = mult * num;    
}
Andy Nonomous
  • 49
  • 1
  • 8
  • 1
    `strtol()`? `sscanf()`? *Anything but the code you've written?* – EOF Aug 12 '15 at 13:59
  • 2
    @SouravGhosh: No. `strtol()` needs no `strtok()`, since it can return the end of the current number in the `char **endptr`-argument. – EOF Aug 12 '15 at 14:01
  • [Has close resemblance to this one.](http://stackoverflow.com/questions/7021725/converting-string-to-integer-c) – ameyCU Aug 12 '15 at 14:06
  • `while(strcmp(&TEMPS[c], "\0") != 0)` `while(TEMPS[c] != '\0')` – Ôrel Aug 12 '15 at 14:07
  • 2
    Thanks for the advice. No need to be hostile EOF, I'm new and don't know what I'm doing, that's why I stated at the beginning that my solution was ridiculous. – Andy Nonomous Aug 12 '15 at 14:18
  • @AndyNonomous: yeah, sorry if I'm being a bit abrasive. The magic numbers in your code triggered me. – EOF Aug 12 '15 at 14:19

1 Answers1

2

I would use strtol - here's a good site for how it works and examples

http://www.tutorialspoint.com/c_standard_library/c_function_strtol.htm

long int strtol(const char *str, char **endptr, int base)

Parameters

  • str -- This is the string containing the representation of an integral number.
  • endptr -- This is the reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value.

  • base -- This is the base, which must be between 2 and 36 inclusive, or be the special value 0.

Return Value

  • This function returns the converted integral number as a long int value, else zero value is r

I've included one example from the site.

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

int main()
{
   char str[30] = "2030300 This is test";
   char *ptr;
   long ret;

   ret = strtol(str, &ptr, 10);
   printf("The number(unsigned long integer) is %ld\n", ret);
   printf("String part is |%s|", ptr);

   return(0);
}
Neil
  • 1,036
  • 9
  • 18