I want to generate 10 unique strings each of size 10 printed on each line. I have made a separate function for fetching random characters - get_random_char() and stored them in temp, but I'm getting an undesirable output.
Following is my approach:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static const char alpha[]={
'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','\0'
};
char get_random_char()
{
int alpha_size=sizeof(alpha)-1;
char A[10];
return alpha[rand() % alpha_size];
}
int main()
{
srand(time(NULL));
char str[10][10];
char A[10],temp[10][10];
int j;
for(int i=0;i<10;i++)
{
for(int q=0;q<10;q++)
{
temp[i][q]=get_random_char();
}
for( j=0;j<=i-1;j++)
{
if(strcmp(str[j],temp[i])==0)
break;
}
if(i==j)
{
strcpy(str[i],temp[i]);
}
else
i--;
}
for(int p=0;p<10;p++)
{
printf("%s\n",str[p]);
//printf("\n");
}
return 0;
}
Output:
LTOJMGAPMRLZSDECCFTJTLWEFRVFGWURSLBERDVDUIDOLHRPPKZIYVNFMIKSEHLWSOCJRXOOHRCVBTKQGJYGGNLSVVMCEAYWODHI
LZSDECCFTJTLWEFRVFGWURSLBERDVDUIDOLHRPPKZIYVNFMIKSEHLWSOCJRXOOHRCVBTKQGJYGGNLSVVMCEAYWODHI
TLWEFRVFGWURSLBERDVDUIDOLHRPPKZIYVNFMIKSEHLWSOCJRXOOHRCVBTKQGJYGGNLSVVMCEAYWODHI
URSLBERDVDUIDOLHRPPKZIYVNFMIKSEHLWSOCJRXOOHRCVBTKQGJYGGNLSVVMCEAYWODHI
UIDOLHRPPKZIYVNFMIKSEHLWSOCJRXOOHRCVBTKQGJYGGNLSVVMCEAYWODHI
ZIYVNFMIKSEHLWSOCJRXOOHRCVBTKQGJYGGNLSVVMCEAYWODHI
EHLWSOCJRXOOHRCVBTKQGJYGGNLSVVMCEAYWODHI
OOHRCVBTKQGJYGGNLSVVMCEAYWODHI
GJYGGNLSVVMCEAYWODHI
MCEAYWODHI
randif you want to do anything related to cryptography. Better yet, don't userandever.