Chaine de caracteres aléatoire

winwarrior Messages postés 654 Date d'inscription jeudi 3 avril 2003 Statut Membre Dernière intervention 10 février 2009 - 4 mai 2004 à 19:40
cs_nayline Messages postés 2 Date d'inscription jeudi 6 avril 2006 Statut Membre Dernière intervention 21 février 2009 - 21 févr. 2009 à 16:39
Bon voila je voudrai simplement que ce code retourne une chaine de caracteres aléatoire je comprend pas ce qui cloche nlors de la compilation ça me marque
error C2228: left of '.substr' must have class/struct/union type
(je compile avec VC++)
char *lrand(int num) {
   char resultat[64];
   srand(time(0));
   for(int i=0; i<num; i++) {
        int numero = (1 + rand() % 26);
        char *lettre = ("abcdefghijklmnopqrstuvwxyz".substr(numero,numero+1));
      	sprintf(resultat,"%s%s",resultat,lettre);
   }
   return (char *) resultat;
}

merci d'avance !
win
A voir également:

6 réponses

cs_djl Messages postés 3011 Date d'inscription jeudi 26 septembre 2002 Statut Membre Dernière intervention 27 novembre 2004 7
4 mai 2004 à 22:58
ca genere une chaine aleatoire (sans chiffres)

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

void RandomString(char *str, size_t n)
{
char c;
int i;
for(i=0;i<n;i++)
{
do
{
c=rand() % 'z';
}while( !(( 'A'<=c && c<='Z' ) || ( 'a'<=c && c<='z' )) );
str[i]=c;
}
str[i]=0;
}

main()
{
srand( time(NULL) );
char str[20];

for(;;)
{
RandomString(str,(sizeof str)-1);
puts(str);
fflush(stdout);
getchar();
}

return 0;
}
1
Rejoignez-nous