fasaw
fasaw

Reputation: 3

Using atoi() funct in C

I wanna convert string to integer.But my string is 234,23,34,45.If i use atoi, it gives me only 234.I wanna convert all integers in my string.How can i use atoi to solve this or what can i use instead of atoi??

Upvotes: 0

Views: 4355

Answers (6)

Mikel
Mikel

Reputation: 25656

Assuming you want {234,23,34,45}.

Using strchr

#include <string.h>

void print_nums(char *s)
{
    char *p;

    for (p = s; p != NULL; p = strchr(p, ','), p = (p == NULL)? NULL: p+1) {
        int i = atoi(p);
        printf("%d\n", i);   /* or whatever you want to do with each number */
    }
}

or perhaps easier to read:

void print_nums(char *s)
{
    char *p = s;            /* p always points to the first character of a number */

    while (1) {
        int i = atoi(p);
        printf("%d\n", i);  /* or whatever you want to do with each number */

        p = strchr(p, ','); /* find the next comma */
        if (p == NULL)
            break;  /* no more commas, end of string */
        else
            p++;    /* skip over the comma */
    }
}

Using strtok

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

void print_nums(const char *str)
{
    char *tempstr = strdup(str);
    char *p = NULL;
    const char *delim = ",";

    for (p = strtok(tempstr, delim); p != NULL; p = strtok(NULL, delim)) {
        int i = atoi(p);
        printf("%d\n", i);  /* or whatever you want to do with each number */
    }

    if (tempstr != NULL) {
        free(tempstr);
        tempstr = NULL;
    }
}

Upvotes: 1

Andy Thomas
Andy Thomas

Reputation: 86509

One option would be to use strtok() to break your string into pieces, then use atoi() on each.

Edit: (Recommended by dmckee in the comments)

  • Warning #1: strtok keeps a pointer to the string between calls; it's not thread-safe.
  • Warning #2: strtok mangles the string passed to it, putting null characters in place of delimiters at the ends of tokens.

Upvotes: 4

Eric Chai
Eric Chai

Reputation: 650

int my_atoi(const char * str) {

  if (!str)
    return 0; // or any other value you want

  int str_len = strlen(str);
  char *num_str = (char *)malloc(str_len * sizeof(char));

  int index = 0;
  for (int i = 0; i < str_len; ++i) {
    char ch = str[i];

    if (ch == 0) {
      num_str[index] = 0;
      break;
    }

    if (isdigit(ch))
      num_str[index++] = ch;
  }
  num_str[index] = 0;

  int ret = atoi((const char *)num_str);
  free(num_str);
  return ret;
}

then call my_atoi(const char *) function:

char *str = "234,23";
int v = my_atoi(str);

Upvotes: 0

Mikel
Mikel

Reputation: 25656

Why don't you normalize the string first?

Here's an (untested) function to do that.

#include <ctype.h>
#include <string.h>

/*
 * remove non-digits from a string
 *
 * caller must free returned string
 */
char *normalize(char *s)
{
    int i, j, l;
    char *t;
    l = strlen(s);
    t = malloc(l+1);
    for (i = 0, j = 0; i < l; i++) {
        if (isdigit(s[i]))
            t[j++] = s[i];
    }
    t[j] = '\0';
    return t;
}

then instead of

int intvalue = atoi(numstring);

do this

char *normalized = normalize(numstring);
int intvalue = atoi(normalized);

Upvotes: 0

dko
dko

Reputation: 904

since a string is nothing but a char * advance a temp char * after every call to atoi to the next instance of a ',' + 1

Upvotes: 1

EnabrenTane
EnabrenTane

Reputation: 7466

You could parse the string and split it on "," then pass the range to atoi().

Upvotes: 0

Related Questions