-2
In this code I am trying to make a string read and then put each word separated by space in an array of strings. I saw that in some cases the Strtok function is used, but since I intend to use these strings for other functions, I don’t know how to use Strtok correctly. In code the variable "strings" is not receiving correctly the characters of the "string", which is wrong?
#include <stdio.h>
#include <string.h>
int main()
{
int N, i, j, k=0, l=0;
scanf ("%d", &N); //número de casos de teste
for (i=1; i<=N; i++)
{
char string[50];
int palavras=1;
fflush(stdin);
fgets (string, 50, stdin);
for(j=0; string[j]!='\0'; j++)
{
if (string[j]==' ')
{
palavras++;
}
}
char strings[palavras][50];
for (j=0; string[j]!='\0'; j++)
{
if (string[j] == ' ')
{
l=0;
k++;
break;
}
else
{
strings[k][l] = string[j];
l++;
}
}
for (j=0; j<palavras; j++)
{
puts (strings[j]);
}
}
return 0;
}
I believe this is in Python, so I can not apply this method in my code
– Matheus