1

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
2
  • Obligatory: don't use rand if you want to do anything related to cryptography. Better yet, don't use rand ever. Commented Dec 3, 2020 at 16:28
  • Thank you so much, that was really helpful! Commented Dec 3, 2020 at 17:11

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much!! This approach seems much easier.
Sometimes keeping it simple is the way to go. Good luck with your coding! For improved readability you can use the character literal for the offset, e.g. randstrs[j][i] = rand() % 26 + 'A';
2

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'.

2 Comments

Couldn't get you, where should I make changes?
When you construct your "random strings", the last character in the array must be '\0'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.