Estou tendo dificuldade em colocar uma string em uma matriz.
O objetivo do meu programa é gerar strings aleatórias. E eu quero que essas strings aleatórias geradas sejam colocadas em uma matriz para que eu possa acessar essas strings novamente mais tarde.
char main()
{
srand(time(NULL));
char* rStr[9] = {0};
int i;
for (i = 0; i < 7; i++)
{
randomString(rStr, 8); //This is the function that generates a
//random string with 8 characters
printf("%d---%s\n", i, rStr);
}
}
O código acima irá produzir 7 strings geradas aleatoriamente com 8 caracteres. Mas meu problema é que quero que todas as strings geradas sejam colocadas em uma matriz para que eu possa chamá-las ou exibi-las novamente.
Eu tentei isso abaixo:
char main()
{
srand(time(NULL));
char* rStr[9] = {0};
int i;
for (i = 0; i < 7; i++)
{
rStr[i] = randomString(rStr, 8);
printf("%d---%s\n", i, rStr[i]);
}
}
mas meu programa simplesmente travava.
EDITAR:
int randomNumber(int min, int max) //This function is responsible for
//the randomness of the string
{
max -= min;
return (rand() % max) +min;
}
char randomString(char *str, int randomCharCount)
{
const char *charSet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
for (i = 0; i < randomCharCount; i++)
{
str[i] = charSet[randomNumber(0, 35)];
}
}
Créditos do código para o tutorial aqui:
Seu código tem os seguintes problemas
return
tipo para main()
.strings
.Experimente o seguinte código: -
char main()
{
srand(time(NULL));
char rStr[9] = {0};
// sStr is where the rStr are saved .
char sStr[50][9]; // max 50 rStr
int i;
for (i = 0; i < 7; i++)
{
randomString(rStr, 8);
strcpy(sStr[i],rStr);
printf("%d---%s\n", i, sStr[i]);
}
// some return value here.
return 0;
}
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras