Printing predefined sentences, randomly in C

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

Does anyone know how to give me an idea? As long as it is easy to understand for a beginner.

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

Note: it is not necessary to pass the entire code, it is enough just to explain to me what is the best solution for the implementation of the part of the sentences.

Author: Misael, 2017-06-05

1 answers

First, since you already have the notion of vector, you know that putting multiple character vectors (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 maximum with 100 characters each. It has a more efficient way, through pointers and dynamic memory allocation, but for this problem we do not need it.

To receive random numbers, you can use the rand() function of the library and then play the first Matrix 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_WORDS which indicates that in this case the maximum value is 10 and does not vary.

If you want to use pointers and have a better use of memory so as not to leave 87 empty characters in the case of " Hello world!". Thus, 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 '\0' no final     */
    palavra = aloca_memoria(quantidade_carac_palavra);
    grava_palavra(palavra, "Hello World!");
    printf("%s", palavra);
    free(palavra);
    return 0;
}

So in a similar way it works to put random numbers and get sentences random.

EDIT:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char *grava(const char *str)
{
    char *pont;
    int i, tamanho = 0;
    while(*(str+tamanho) != '\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;
}
 3
Author: Carlos Adir, 2017-06-07 13:12:05