Print pre-defined phrases, randomly in C

0

Hello, I'm a beginner in programming and am learning to program in C. I want to make a program that has some already defined phrases. Ex with vectors: char vet1[100] = {"Hello World!"}; char vet2[100] = {"segunda frase"}; ... and with each execution, return a different phrase on the user's screen . The closest I came to was to think of using enum in conjunction with rand(); , but it does not work because it returns only a constant with a decimal value. I think the solution would involve using multiple vetores ou uma matriz de strings , but I do not know if that's the case.

Can anyone give me an idea? As long as it's easy to understand for a beginner.

I have already learned basic contents, and also vectors, arrays, functions, pointers, dynamic allocation and files.

NOTE: It is not necessary to pass the whole code, just explain to me what is the best solution for implementing the part of the sentences.

    
asked by anonymous 05.06.2017 / 15:25

1 answer

3

First of all, since you already have the concept of a vector, you know that putting multiple strings is tiring. So I advise using an array:

int main()
{
    char minhas_palavras[10][100];
    return 0;
}

With this I can store 10 words of a maximum of 100 characters each. It has a more efficient way, through pointers and dynamic memory allocation, but for this problem we do not need that.

To receive random numbers, you can use the rand () function of the library and then play the first array index. For example:

int main()
{
    const int QUANTIDADE_PALAVRAS = 10;
    char minhas_palavras[QUANTIDADE_PALAVRAS][100];
    int numero_aleatorio;
    /* Aqui voce define suas palavras */
    numero_aleatorio = rand() % QUANTIDADE_PALAVRAS;
    printf("Palavra escolhida: %s", minhas_palavras[numero_aleatorio]);
    return 0;
}

Where we use a constant called QUANTITY_PALAVRAS which indicates that in this case the maximum value is 10 and does not vary.

If you want to use pointers and have better memory usage, do not leave 87 empty characters in the case of "Hello World!". So, the code below exemplifies this idea:

void grava_palavra(char *str, const char *palavra);
/* Pega todos os caracteres de *palavra e grava em *str
   assim como feito pela função strcopy */
char *aloca_memoria(int quantidade);
/* Essa funcao recebe um inteiro como argumento e retorna
   um ponteiro para caracter que indica onde foi alocada a memoria*/
int main()
{
    char *palavra;
    int quantidade_carac_palavra;
    /* Aqui voce define o tamanho da sua palavra. Ex: 13
       Não se esqueca do caracter '
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char *grava(const char *str)
{
    char *pont;
    int i, tamanho = 0;
    while(*(str+tamanho) != '
int main()
{
    char minhas_palavras[10][100];
    return 0;
}
') tamanho++; pont = (char *)malloc((tamanho+1)*sizeof(char)); for(i=0; i<tamanho+1; i++) *(pont+i) = *(str+i); return pont; } char **grava_matriz(int quantidade) { return (char **) malloc(quantidade*sizeof(char*)); } int main() { char **mat; int ale, q = 2; mat = grava_matriz(q); *(mat) = grava("Hello"); *(mat+1) = grava("hey"); srand((unsigned)time(NULL)); ale = rand()%q; printf("%s\n", *(mat+ale)); free(*(mat)); free(*(mat+1)); free(mat); return 0; }
' no final */ palavra = aloca_memoria(quantidade_carac_palavra); grava_palavra(palavra, "Hello World!"); printf("%s", palavra); free(palavra); return 0; }

So, it works similarly to random numbers and random phrases.

EDIT:

int main()
{
    const int QUANTIDADE_PALAVRAS = 10;
    char minhas_palavras[QUANTIDADE_PALAVRAS][100];
    int numero_aleatorio;
    /* Aqui voce define suas palavras */
    numero_aleatorio = rand() % QUANTIDADE_PALAVRAS;
    printf("Palavra escolhida: %s", minhas_palavras[numero_aleatorio]);
    return 0;
}
    
05.06.2017 / 17:49