0

I wanna ask how it can not get integer from a string

for example, here are my code:

int main() {
    char str[] = "ababbababa-1998";
    int nr = atoi(str);
    printf("%d\n", nr);
    return (EXIT_SUCCESS);
}

when running, it print out 0 but not 1998, how can I fix it ?

STEPHEN bui
  • 579
  • 1
  • 9
  • 27

4 Answers4

1

In your case you can use strtok.

int main() {
    char str[] = "ababbababa-1998";
    char * const first_part = strtok(str, "-");
    if (first_part == NULL) {
        return 1;
    }
    char * const second_part = strtok(NULL, "-");
    if (second_part == NULL) {
        return 1;
    }
    int nr = atoi(second_part);
    printf("%d\n", nr);
    return 0;
}

You can look at Why is there no strtoi in stdlib.h? for error check atoi.

Community
  • 1
  • 1
Stargateur
  • 24,473
  • 8
  • 65
  • 91
1

Keep walking down str() until code finds something numeric using strtol().

int main() {
    char str[] = "ababbababa-1998";
    char *p = str; 

    char *endptr;
    while (*p) {
      long number = strtol(p, &endptr, 10);
      // Was conversion successful?
      if (endptr != p) {
        printf("%ld\n", number);
        return EXIT_SUCCESS;
      }
      p++;
    }
    puts("No conversion");
    return EXIT_FAILURE;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define ASCII '0'

int
main(void) {
    char const str[] = "ababbababa-1998";
    int i, result = 0;

    for (i = 0; str[i]; i++) {
        if (isdigit(str[i])) {
            result *= 10;
            result += str[i] - ASCII;
        }
    }

    printf("number = %d\n", result);

    return 0;
}
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
  • It may be worth using a larger `INITSIZE` (e.g. 128, 256, etc..) and use a static buffer to avoid the `malloc`, `realloc` overhead. (there isn't anything wrong with dynamic allocation, it just adds overhead and complexity). You may also want to stop adding to `nr` after you have found digits, and then find a subsequent non-digit. (e.g. if you had a string `"aaaaa-1998aaaaa-1999"`) You could use a simple flag for that purpose. Just thoughts, not criticisms. – David C. Rankin Dec 01 '16 at 05:03
  • Yeah thanks for suggestion @DavidC.Rankin, I think this approach is too complicated. I will edit my answer. – RoadRunner Dec 01 '16 at 05:04
  • Copy most of the string just to convert it to a number? Rubbish! `if (isdigit(array[i]) { total *= 10; total += array[i]; }` – John3136 Dec 01 '16 at 05:19
  • Changed it @John3136. I overlooked this simple approach, cheers. – RoadRunner Dec 01 '16 at 05:34
  • 1
    Obviously that should be `total += array[i] - '0';` - Just notcied you already did that. – John3136 Dec 01 '16 at 08:20
0

If you want to extract all the numeric digits from a string you could use this function I created.

You will need these header files for this function to work.

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void getNumbers(char data[]) {
   int index = 0;
   char current;

   for( int i = 0; i < strlen(data); ++i ) {
      current = data[i];
      if (current >= 48 && current <= 57) {
         data[index++] = current;
      }
   }
    data[index] = '\0';
}

You can use the above function like this.


char foobar[] = "1A2B3C4D5E6F7G8H9I";

getNumbers(foobar);

printf("%s", foobar);

The above code will output 123456789