在 C 中使用 atoi() 函数

发布于 2024-10-08 09:23:17 字数 109 浏览 7 评论 0原文

我想将字符串转换为整数。但是我的字符串是 234,23,34,45。如果我使用 atoi,它只给我 234。我想转换字符串中的所有整数。我如何使用 atoi 来解决这个问题或者什么可以我用而不是atoi?

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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

琉璃繁缕 2024-10-15 09:23:17

一种选择是使用 strtok() 将字符串分成几部分,然后对每个使用 atoi() 。

编辑:(dmckee 在评论中推荐)

  • 警告#1:strtok 在调用之间保留指向字符串的指针;它不是线程安全的。
  • 警告 #2:strtok 会破坏传递给它的字符串,在标记末尾放置空字符来代替分隔符。

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.
痴情 2024-10-15 09:23:17

因为字符串只不过是一个 char * 在每次调用 atoi 到 ',' + 1 的下一个实例之后前进一个临时字符 *

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

流心雨 2024-10-15 09:23:17

假设您想要 {234,23,34,45}。

使用 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 */
    }
}

或者可能更容易阅读:

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 */
    }
}

使用 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;
    }
}

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;
    }
}
久而酒知 2024-10-15 09:23:17

您可以解析字符串并将其拆分为“,”,然后将范围传递给 atoi()。

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

寒江雪… 2024-10-15 09:23:17

为什么不先规范化字符串呢?

这是一个(未经测试的)函数来做到这一点。

#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;
}

然后而不是

int intvalue = atoi(numstring);

这样做

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

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);
撧情箌佬 2024-10-15 09:23:17
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;
}

然后调用 my_atoi(const char *) 函数:

char *str = "234,23";
int v = my_atoi(str);
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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文