hi there ..... would like to know how would be the coding for password generation of different sets of characters... thanks u

Not sure I understand your question... so this may be way off base.

Different sets of characters as in uppercase, lowercase, numbers, and symbols or a completely different character set?

To generate a password, you will need to know a defined length you want, then generate random numbers which correspond to decimal value of a character you want from the ASCII table. When that number is generated, display that character.

the password consists of 6 different characters where Different sets of characters as in uppercase, lowercase, numbers, and symbols .....

Then the above suggestion would work. Randomly generate a number within a certain ASCII range then display the corresponding character. Have you attempted this?

>Randomly generate a number within a certain ASCII
>range then display the corresponding character.
First, that's not a portable solution. Second, it's not an elegant solution either because you end up screwing around with multiple ranges and basically playing number games. A much better solution is to store all of the characters you want in an array and then select a random index. The code to do this is trivial if you don't want any probability or guaranteed character classes to be included in the password:

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

#define PASSWORD_SIZE 6

static const char *charset =
  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  "abcdefghijklmnopqrstuvwxyz"
  "0123456789";

int main ( void )
{
  size_t len = strlen ( charset );
  int i;

  for ( i = 0; i < PASSWORD_SIZE; i++ )
    putchar ( charset[rand() % len] );

  puts ( "" );

  return 0;
}

If you want some characters to be more probable than others, or your password rules require certain character types (at least one upper case, at least one lower case, and at least one number, for instance), it gets harder.

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

int main(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
    
    int passChar;
    
    long long counter = 0;
   
    float tempRand;
    
    srand(time(NULL));

//If 1 argument or more have been passed to this program, we'll actually generate a password
    if (argc > 1) {
       
        long long count  = strtoll(argv[1],NULL,10);
        
        while (counter < count) {
            
            tempRand = (float) rand() / RAND_MAX;
            //If tempRand if smaller than 0.7, we're gonna generate a alphabetical character
            if (tempRand < .7) {
               
                passChar = ((float) rand()/RAND_MAX) * 26;
                
                if (tempRand < .35) {
                    passChar += 'a';
                } else {    //Otherwise it's gonna be an uppercase one
                    passChar += 'A';
                }
            } else {        //If temprand was bigger than 0.7, we generate a numerical character
                passChar = ((float) rand()/RAND_MAX) * 9;
                passChar += '0';
            }
      
            putchar(passChar);
            
            counter++;
        }
        
        putchar('\n');
    } else {        //If no arguments were passed to this program, we give a little help to figure out how this works.
        printf("Usage:\n\t%s <length>\n\t\t<length> is a positive integer, the password will have this length.\n",argv[0]);
    }
    
    return 0;

oh thanks a lot.. how bout this code... i couldnt run the code?

>i couldnt run the code?
I'm not surprised. The structure is broken in such a way that I'm inclined to believe you didn't write it. Otherwise you would be able to find and fix the two immediate problems (a doubled function header for main and a missing closing brace for main) without any trouble.

You'll also find that long long doesn't work on some compilers, and strtoll will be equally troublesome. Support for long long is only available on C99 compilers and as an extension to older compilers, where the semantics may be different. Somehow I doubt you really need the range of a long long, and your code would be more portable using long and strtol.

>> "Second, it's not an elegant solution either because you end up screwing around with multiple ranges and basically playing number games"

It works for me... I left out mutiple details in order to not cloud the point. My needs were to have at least two characters of each type and a certain length, plus it had to start with an alpha and there could also not be consecutive leters, or sequential numbers.... among a plethora of other requirements. Elegance is relevant to the creator and due to tight regulations took a back seat on my particular solution.

commented: 'It works for me'... words any programmer worth their salt doesn't want to hear. -2

>Elegance is relevant to the creator and due to tight regulations took a back seat
Elegance probably would have saved you a lot of unnecessary work. But you go ahead and believe what you want to.

I will, thanks. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.