0
I would like to know how I can store the return of the function below, which generates a random string, in a vector of strings with 100 of these words generated by the function, to then make a sort - increasing and decreasing - in the vector.
char* geraStringAleatoria(){
char *validchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *novastr;
register int i;
int str_len;
// novo tamanho
str_len = (rand() % MAX_STR_SIZE );
// checa tamanho
str_len += ( str_len < MIN_STR_SIZE ) ? MIN_STR_SIZE : 0;
// aloca memoria
novastr = ( char * ) malloc ( (str_len + 1) * sizeof(char));
if ( !novastr ){
printf("[*] Erro ao alocar memoria.\n" );
exit ( EXIT_FAILURE );
}
// gera string aleatória
for ( i = 0; i < str_len; i++ ) {
novastr[i] = validchars[ rand() % strlen(validchars) ];
novastr[i + 1] = 0x0;
}
return novastr;
}
In the main function I can assign the return of the function to a string:
int main() {
char *str;
str = geraStringAleatoria();
return 0;
}
But I don’t know how to store each generated word in a string array, as if each vector position were a word.
what have you tried? what problems are you facing?
– mercador
I tried to store the function return in a string array: char words[ i ][ j ] = generatStringAleatoria(); - i being the number of characters and j the number of words. But I think this doesn’t make much sense, I don’t know how to do I’m beginner, I believe I’m assigning different types, and I don’t know how to store the function return in a vector with the generated words
– Caio Teixeira
Another thing: Identation is not a toy. Use, and use it in a way that makes it easier to read your program. If you are too lazy, there are several tools to do this automatically, both in the editors used for programming and online. The only purpose of identation in a C-porgram is to make reading the program easier. If you use inconsistent identation, it gets harder rather than easier.
– jsbueno
Gold detail, in its function of generating strings, the line
novastr[i + 1] = 0x0;
can stay out of thefor
. There is no problem the string being inconsistent while being generated (unless it was a multi-thread program, and there was another code trying to read the same data structure in parallel)– jsbueno