Home > Software engineering >  Put random value from array in other array letter by letter
Put random value from array in other array letter by letter

Time:11-10


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

void initRandom();

void initRandom(){
    srand(time(NULL));
}

int intUniformRnd(int a, int b){
    return a   rand()%(b-a 1);
}

const char* animaisQuatro[] = {"gato", "urso","vaca"};


int main() {
    int opcao;
    char letra;
    menu();
    scanf("%i", &opcao);
    char quatro[4] = {'*' , '*' , '*', '*'};

    if(opcao == 1) {
        int catOpc, palOpc;
        menuCategorias();
        scanf("%d", &catOpc);
        if (catOpc == 1) {
            menuPalavras();
            scanf("%d", &palOpc);
            if (palOpc == 1) {
                initRandom();
                printf("%s\n", animaisQuatro[intUniformRnd(0,2)]);
                for(int i=0;i<4;i  ){
                    printf("%c", quatro[i]);
                }
        }
    }

return 0;
}

I have this code that give me a random animal from the array const char* animaisQuatro[] = {"gato", "urso","vaca"}; from here

                initRandom();
                printf("%s\n", animaisQuatro[intUniformRnd(0,2)]);

and then I want to put that random animal in other array letter by letter but I dont know how

CodePudding user response:

First I reduced your code to a minimal and reproducible example (something you should do whenever you ask a question):

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

void initRandom() {
    srand(time(NULL));
}

int intUniformRnd(int a, int b){
    return a   rand() % (b-a 1);
}

const char* animaisQuatro[] = {"gato", "urso","vaca"};

int main() {
    char quatro[4] = {'*' , '*' , '*', '*'};

    initRandom();
    printf("%s\n", animaisQuatro[intUniformRnd(0,2)]);
    for(int i=0;i<4;i  ){
        printf("%c", quatro[i]);
    }
    
    return 0;
}

Then you can proceed like this:

int main() {
    char quatro[4] = {'*' , '*' , '*', '*'};

    initRandom();
    
    // Get the animal name from a random position
    char* name = animaisQuatro[intUniformRnd(0, 2)];
    
    // Iterate four times
    for (int i = 0; i < 4; i  ) {
        // Assign each `name` index to its respective `quatro` position
        quatro[i] = name[i];
    }
    
    printf("%s", quatro);
    
    return 0;
}

Tip: you can avoiding hardcoding the 2 when calling intUniformRnd. Note that

printf("%d\n", (int) sizeof(animaisQuatro));
printf("%d\n", (int) sizeof(char*));
printf("%d\n", (int) sizeof(animaisQuatro) / sizeof(char*));

outputs

24
8
3

Therefore, you can do

int length = (int) sizeof(animaisQuatro) / sizeof(char*);
int pos = intUniformRnd(0, length - 1);

This way, if you want to add more elements to animaisQuatro, you don't need to change the value inside intUniformRnd.

CodePudding user response:

I want to put that random animal in other array letter by letter

To copy a string to another character array, code could use

// Risky
strcpy(quatro, animaisQuatro[intUniformRnd(0,2)]);

That would overflow quatro[] if it is too small and leads to undefined behavior. (Bad)


A better way to copy and prevent buffer overflow and alert of a failure:

int len = snprintf(quatro, sizeof quatro, "%s", animaisQuatro[intUniformRnd(0,2)]);
if (len >= sizeof quatro) {
  fprintf(stderr, "quatro too small.\n");
}

Since C99 and selectively afterword, code could use a variable length array to form a right-size quatro array.

const char *animal = animaisQuatro[intUniformRnd(0,2)];
size_t sz = strlen(animal)   1;
char quatro[sz];
strcpy(quatro, animal);

Yet since intUniformRnd[] is constant, no need to copy the text, just copy the address to a pointer:

const char *quatro = animaisQuatro[intUniformRnd(0,2)];
  •  Tags:  
  • c
  • Related