Separate a single string in an array of strings

Asked

Viewed 604 times

-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;
}

1 answer

-1

Hi, buddy There would be no way to use the "String.split()" method in your application ?

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

array[0] -> name;

  • 1

    I believe this is in Python, so I can not apply this method in my code

Browser other questions tagged

You are not signed in. Login or sign up in order to post.