#include <stdio.h>
#include <stdint.h>
int is_digit(char c){
return (c >= '0' && c <= '9');
}
void parse_csv_to_array(const char *str, uint8_t *arr, uint8_t *count) {
uint8_t i = 0; // Index for iterating through the string
uint16_t current_num = 0; // Accumulator for building the number
uint8_t has_number = 0; // Flag to track if we are currently parsing a number
*count = 0; // Ensure the array index starts at 0
while(str[i] != '\0'){
if(is_digit(str[i])){
// Multiply by 10 to shift the digit left (handles multi-digit numbers like 12)
current_num = (current_num * 10) + (str[i] - '0');
has_number = 1;
}
else if (str[i] == ',') {
// When we hit a comma, save the built number to the array
if (has_number) {
arr[*count] = (uint8_t)current_num;
(*count)++; // Move to the next slot in the array
current_num = 0; // Reset the accumulator for the next number
has_number = 0; // Reset the flag
}
}
i++;
}
// Catch the final number in the string (since there is no comma at the very end)
if (has_number) {
arr[*count] = (uint8_t)current_num;
(*count)++;
}
}
int main() {
char str[101];
fgets(str, sizeof(str), stdin);
// Remove newline
uint8_t i = 0;
while (str[i]) {
if (str[i] == '\n') {
str[i] = '\0';
break;
}
i++;
}
uint8_t arr[20];
uint8_t count = 0;
parse_csv_to_array(str, arr, &count);
for (uint8_t i = 0; i < count; i++) {
printf("%u", arr[i]);
if(i < count - 1){
printf(" ");
}
}
return 0;
}
Input
10,20,30
Expected Output
10 20 30