Separate a single string into an array of strings [duplicate]

-2

In this code I'm trying to make a string read and then put each space-separated word 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 do not know how to use strtok correctly. In the code the variable "strings" is not receiving the characters of the string correctly, what 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]!='
#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]!='%pre%'; j++)
    {
        if (string[j]==' ')
        {
            palavras++;
        }
    }
    char strings[palavras][50];
    for (j=0; string[j]!='%pre%'; 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;
}
'; j++) { if (string[j]==' ') { palavras++; } } char strings[palavras][50]; for (j=0; string[j]!='%pre%'; 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; }
    
asked by anonymous 19.09.2018 / 22:39

1 answer

-1

Hi, my friend. Could not you use the "String.split ()" method in your application?

String frase = "nome teste 10"; String array[] = new String[3]; array = frase.split(" ");

array [0] - > name;

    
19.09.2018 / 23:06