Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- // Gera senha aleatoria
- // Autor Alexandre Campos
- // Gera 1 numero aleatorio ate um limite
- int numero_aleatorio(int maximo)
- {
- return random() % maximo;
- }
- char * gera_senha(int tamanho){
- // Aloca o espaco
- char * resultado = malloc(tamanho * sizeof(char));
- char permitidos[] = "23456789qwertyuiopasdfghjkzxcvbnmQWERTYUPASDFGHJKLZXCVBNM!@#$";
- // strlen calcule o tamanho de uma string data
- int tamanho_dos_permitidos = strlen(permitidos);
- // Preencher a senha com caracteres aleatorios
- for (int i=0; i<tamanho; i++){
- // Gera posicao aleatoria
- int posicao_aleatoria = numero_aleatorio(tamanho_dos_permitidos);
- // Na posicao i da senha, coloca um caracter da posicao aleatoria
- resultado[i] = permitidos[posicao_aleatoria];
- }
- return resultado;
- }
- int main()
- {
- // Chamar 1 vez
- srand(time(NULL));
- // Declaracao
- int tamanho_da_senha = 10;
- char * senha = gera_senha(tamanho_da_senha);
- // Exibe a senha
- printf("Senha: %s\n", senha);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement