I want to read a string from stdin, save it in a array, convert so it matches the specified test(s):
expected = "45 76 65 72 79 20"
I've already tried every solution I could find since Friday, except strtol
which I don't understand how to use.
char input;
char *buffer = NULL;
char chunk[2];
size_t stringLength = 0;
int n = 0;
while(fgets(chunk, 2, stdin) != NULL){
stringLength += strlen(chunk);
}
rewind(stdin);
buffer = (char *) malloc(stringLength + 1);
if (buffer == NULL)
exit(EXIT_FAILURE);
while(scanf("%c", &input) == 1) {
snprintf(buffer+n, 2, "%c", input);
n += strlen(chunk);
}
//code to convert char array buffer to array of hex separated by spaces
Example text received from stdin = "Every P";
String I need to output to pass the example test: = "45 76 65 72 79 20 50";
Please tell me if I'm making any mistakes, I've been learning how to write C-code for 1 1/2 months.
Thanks in advance!