[英]Get numbers separated by commas
我得到以下字符串:
"312 ,22 ,+12 , -12 , 5331"
数字之间最多可以有1个空格。
我需要将其转换为这样的数组:
int arr[] = {312,22,-12,12,5331};
使用C89可以做到这一点吗?
# 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;
}
建议:
strtok()
将字符串拆分为令牌。 atoi()
将标记转换为int
。 为了分配数组存储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
是的,您可以使用sscanf
函数将整数放入数组元素中。 我假设这里的字符串中只有少量固定的整数。
我不是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 */
为什么不重复使用sscanf(str+offset, "%d,%n", &newValue, &offset)
直到失败。
我认为没有任何标准功能可以执行此操作。 这是一种常见的操作,大多数程序员的个人工具包中都包含以下代码。 答案在于使用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 * /
希望这会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.