Reputation: 55
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
Upvotes: 1
Views: 1697
Reputation: 84521
Another way to make things a bit easier, is to initialize your 10x11 2D array all zero to begin with (so you ensure a nul-terminating character after each 10-character string). Then just populate your array in some reasonable way with random characters within your range (all upper-case), e.g.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (void) {
char randstrs[10][11] = {{0}}; /* 2D array initialized all zero */
srand (time(NULL)); /* seed random number geenrator */
for (int i = 0; i < 10; i++) /* fill 2D array columnwise */
for (int j = 0; j < 10; j++)
randstrs[j][i] = rand() % 26 + 65; /* upper case characters */
// randstrs[j][i] = rand() % 95 + 32; /* all printable chars */
for (int i = 0; i < 10; i++) /* output results */
printf ("%s\n", randstrs[i]);
}
The code above fills columns of characters rather than strings. Just one alternative to simply filling each string sequentially.
Example Use/Output
$ ./bin/randstr_10
ARGNXRHMVD
VPPDFXTAJX
TAVNFDWNKB
BXPJXOWRBA
HYVECMEZZV
IIXWFYBRYF
TVDRHLUWZD
PJFZZPWHKQ
WHDLOEAQNB
YSSOORINFF
If you use the range for all printable ASCII characters, you would have something similar to:
$ ./bin/randstr_10
Q]xSqIPCdo
],1GoPzYde
2S8>D^)m `
g<}eE_ngCu
]p=/,K`~DQ
ft,{6{5vK>
E<DI:kg}=,
<ANTFl*7-9
q8x5WGf=5I
@#c;Pa|Rdr
Upvotes: 2
Reputation: 81916
In c, strings are terminated by the null character. So if you want 10 visible characters, you need to allocate 11 bytes, and have the last byte be the character '\0'
.
Upvotes: 2