#include <stdio.h>
#include <stdint.h>
uint8_t get_number(char* number_str, uint8_t n){
uint8_t number = 0;
for (size_t i=0; i<n; i++){
uint8_t num = ((uint8_t)*(number_str+i)) - '0';
switch (n-i){
case 3:
num *= 100;
break;
case 2:
num *= 10;
break;
}
number += num;
}
return number;
}
void parse_csv_to_array(const char *str, uint8_t *arr, uint8_t *count) {
// Your logic here
char number_str[3];
uint8_t number_n = 0;
for (size_t i=0; i<255; i++){
char chr = *(str+i);
if (chr != ',' & chr != '\0'){
number_str[number_n] = chr;
number_n++;
} else {
uint8_t number = get_number(number_str, number_n);
*(arr + *count) = number;
(*count)++;
number_n = 0;
if (chr == '\0'){
return;
}
}
}
}
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