My problem is I can't make my program to read char *word
in function read_word_details
and parse it back to solve_task_1
function.
void read_word_details(int *x, int *y, int *orientation, char *word) {
char *p, input[20];
fgets(input, 20, stdin);
p = strtok(input, " ");
*x = string_to_number(p);
p = strtok(NULL, " ");
*y = string_to_number(p);
p = strtok(NULL, " ");
*orientation = string_to_number(p);
p = strtok(NULL, " ");
word = p;
}
void solve_task_1() {
char input[20], *p;
fgets(input, 20, stdin);
p = strtok(input, "a");
int n = string_to_number(p);
for (int i = 1; i <= n; i++) {
int x, y, orientation;
char *word;
read_word_details(&x, &y, &orientation, word);
printf("%d %d %d %s\n", x, y, orientation, word);
}
}
My input is
1
1 2 3 asd
,
and my output is 1 2 3 HH9u[]A\A]A^A_ff.
, instead of 1 2 3 asd
.
Edit: My string_to_number
function is:
int string_to_number(char *p)
{
int length = strlen(p);
int sum = 0;
for (int i = 0; i < length; i++) {
if (p[i] >= '0' && p[i] <= '9') {
sum *= 10;
sum += p[i] - '0';
}
}
return sum;
}