This takes a string input like 1,2,3,4,5
, splits it using ,
delimiter, converts each char to int and output it in a single line like this : 12345
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int* createIntArray(char* numbersInString, int* array_size);
int* createIntArray(char* numbersInString, int* array_size)
{
char* token = strtok(numbersInString,",");
int* numbersArray = malloc(sizeof(int)*100);
int i = 0;
while(token != NULL)
{
numbersArray[i] = (int)*token -'0';
token = strtok(NULL,",");
i++;
}
*array_size = i;
return numbersArray;
}
int main(){
char* stringNumbers = malloc(sizeof(char)*100);
int* intArray;
int arrayLength = 0;
printf("Enter numbers\n");
scanf("%s",stringNumbers);
intArray = createIntArray(stringNumbers,&arrayLength);
int i;
for(i = 0; i < arrayLength;i++)
{
printf("%d",intArray[i]);
}
free(stringNumbers);
free(intArray);
return 0;
}
I know the code is super useless, but I'm starting to learn C and I wanted to see if my code was alright and if it complies to C coding standards.
12379573853625767531596,1
. Is that it? – TopinFrassi yesterday