我收到了以下字符串:
"312 ,22 ,+12 , -12 , 5331"
数字之间可以有多个空格。
我需要将它转换为类似的数组:
int arr[] = {312,22,-12,12,5331};
使用C89是否有一种漂亮而优雅的方法?
答案 0 :(得分:5)
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main(int argc, char const *argv[])
{
char numbers_str[] = "312 ,22 ,+12 ,-12 ,5331", *currnum;
int numbers[5], i = 0;
while ((currnum = strtok(i ? NULL : numbers_str, " ,")) != NULL)
numbers[i++] = atoi(currnum);
printf("%d\n", numbers[3]);
return 0;
}
答案 1 :(得分:1)
推荐:
strtok()
将字符串拆分为令牌。atoi()
将令牌转换为int
s。要分配数组以存储int
,您可以:
realloc()
或malloc()
数组。示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int* make_int_array(char* a_str, size_t* const a_elem_count)
{
int* result = 0;
char* tmp = a_str;
char* last_comma = 0;
/* Count how many ints will be extracted. */
*a_elem_count = 0;
while (*tmp)
{
if (',' == *tmp)
{
(*a_elem_count)++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing int. */
*a_elem_count += last_comma < (a_str + strlen(a_str) - 1);
result = malloc(sizeof(int) * (*a_elem_count));
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, ",");
while (token)
{
assert(idx < *a_elem_count);
*(result + idx++) = atoi(token);
token = strtok(0, ",");
}
}
return result;
}
int main()
{
char s[] = "312 ,22 ,+12 ,-12 ,5331";
int* int_list;
size_t int_list_count = 0;
printf("s=[%s]\n\n", s);
int_list = make_int_array(s, &int_list_count);
if (int_list)
{
size_t i;
for (i = 0; i < int_list_count; i++)
{
printf("%d\n", *(int_list + i));
}
printf("\n");
free(int_list);
}
return 0;
}
输出:
s=[312 ,22 ,+12 ,-12 ,5331]
312
22
12
-12
5331
答案 2 :(得分:0)
是的,您可以使用sscanf
函数将整数放入数组元素中。我假设你的字符串中有一小部分固定的整数。
答案 3 :(得分:0)
我不是C程序员,但ANSI C(或C89)确实有一个名为strtok的“拆分”功能。
#include <string.h>
#include <stddef.h>
...
char string[] = "words separated by spaces -- and, punctuation!";
const char delimiters[] = " .,;:!-";
char *token;
...
token = strtok (string, delimiters); /* token => "words" */
token = strtok (NULL, delimiters); /* token => "separated" */
token = strtok (NULL, delimiters); /* token => "by" */
token = strtok (NULL, delimiters); /* token => "spaces" */
token = strtok (NULL, delimiters); /* token => "and" */
token = strtok (NULL, delimiters); /* token => "punctuation" */
token = strtok (NULL, delimiters); /* token => NULL */
答案 4 :(得分:0)
为什么不重复使用sscanf(str+offset, "%d,%n", &newValue, &offset)
直到它失败。
答案 5 :(得分:0)
我认为没有任何标准功能可以做到这一点。这是一种常见的操作,大多数程序员在他们的个人工具包中都有类似下面的代码。 答案在于使用strtol()函数。我很快就从strtol的手册页中攻击了以下内容:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main (int argc, char *argv[])
{
char sep = ',';
char string[] = " 312 ,, 22 ,+12 ,-12 ,5331";
/*
* count separators
*/
char *str = string;
int j = 0;
while (*str)
{
printf ("str=%c\n", *str);
if (*str == sep)
j++;
str++;
}
int n = j + 1;
printf ("n=%d\n", n);
long int *arr = malloc (n * sizeof (long int));
char *endptr = NULL;
str = string;
j = 0;
do
{
arr[j++] = strtol (str, &endptr, 10);
if (*endptr != '\0')
{
while (*endptr != sep)
endptr++;
str = endptr + 1;
}
}
while (j < n && *endptr);
for (j = 0; j < n; j++)
{
printf ("%d:%ld\n", j, arr[j]);
}
exit (EXIT_SUCCESS);
} / * main * /
希望这是有帮助的