I am trying to generate a name
string by concatenating random vowels and consonants.
I've tried using strcat
to concatenate random vowels/consonants to the name
string, but that results in segmentation fault.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
[...]
char randomVowel() {
char * vowels = "aeiou";
return vowels[randomRange(0, 4)];
}
char randomConsonant() {
char * consonants = "bcdfghjklmnpqrstvxwyz";
return consonants[randomRange(0, 20)];
}
char * randomName() {
int i;
char * name;
for (i = 0; i < randomNumber(20); i++){
i % 2 ? strcat(name, randomVowel()) : strcat(name, randomConsonant());
}
return name;
}
int main () {
printf("%s\n", randomName());
return 0;
}
What is the best approach for this problem?