[英]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.