在c中随机生成一个名字
generate a name in c randomly
signed char *tab_alphabet[]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","[=10=]"};
int nombreAlea(int min, int max){
return (rand()%(max-min+1) + min);
}
void generer_name(int length,signed char* n){
int i ;
signed char *j;
for (i=0;i<length;i++){
int k = nombreAlea(1,26);// from the table of the alphabet
j = tab_alphabet[k-1];
strcat(n,j);
}
}
这是主要内容:
int main () {
int a = nombreAlea(4,30);
signed char *nn;
generer_name(a,nn);
return 0 ;
}
问题是结果总是以“a!!@”开头,任何帮助,我对 strcat
有疑问
您需要使用不同的值调用 srand,经典方法是使用时间。
使用足够大的 char[] 来存储生成的字符串
使用简单的字符串作为字母表。
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
char *tab_alphabet="abcdefghijklmnopqrstuvwxyz";
int nombreAlea(int min, int max){
return (rand()%(max-min+1) + min);
}
void generer_name(int length, char n[]){
int i ;
for (i=0;i<length;i++){
int k = nombreAlea(1,26);// from the table of the alphabet
n[i] = tab_alphabet[k-1];
}
n[i] = '[=10=]';
}
int main (void) {
char nn[64];
int a;
srand( time( NULL ) );
a = nombreAlea(4, 30);
generer_name(a, nn);
printf(" >%s<\n", nn);
return 0 ;
}
signed char *tab_alphabet[]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","[=10=]"};
int nombreAlea(int min, int max){
return (rand()%(max-min+1) + min);
}
void generer_name(int length,signed char* n){
int i ;
signed char *j;
for (i=0;i<length;i++){
int k = nombreAlea(1,26);// from the table of the alphabet
j = tab_alphabet[k-1];
strcat(n,j);
}
}
这是主要内容:
int main () {
int a = nombreAlea(4,30);
signed char *nn;
generer_name(a,nn);
return 0 ;
}
问题是结果总是以“a!!@”开头,任何帮助,我对 strcat
有疑问您需要使用不同的值调用 srand,经典方法是使用时间。
使用足够大的 char[] 来存储生成的字符串
使用简单的字符串作为字母表。
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
char *tab_alphabet="abcdefghijklmnopqrstuvwxyz";
int nombreAlea(int min, int max){
return (rand()%(max-min+1) + min);
}
void generer_name(int length, char n[]){
int i ;
for (i=0;i<length;i++){
int k = nombreAlea(1,26);// from the table of the alphabet
n[i] = tab_alphabet[k-1];
}
n[i] = '[=10=]';
}
int main (void) {
char nn[64];
int a;
srand( time( NULL ) );
a = nombreAlea(4, 30);
generer_name(a, nn);
printf(" >%s<\n", nn);
return 0 ;
}