Generate strings

Hi all,

I want to generate some strings by algorithm.
There are some algorithm that generate alpha-numeric strings?
I don't want something simple since i want to use it to generate some kind of passwords
I'll give you a basic pointer of how you could accomplish this. You could start by making a

char array that contains the letters of the alphabet (or all the keys on the keyboard). Then

generate random numbers, apply them to the array, and add them to a string. It could look

something like this:

#include <iostream>
#include <string>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <cmath>
using namespace std;

int main()
{
srand(time(NULL));
char letters[27]={'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'};
string password;
int length;
int a;
cout<<"How long do you want your password to be? ";
cin>>length;
for(int i=0;i<length;i++){
a=rand()%26;
password+=letters[a];
}
cout<<password<<endl;
system("PAUSE");
return 0;
}



Last edited on
closed account (o1vk4iN6)
You can make it even simpler than that.

1
2
3
4
5
6
7
8
9
10

int main()
{
	int len;
	cin >> len;
	
	while(len--)
		cout << 'a' + rand() % 26;
}


No need for an array since it's redundant seeing as they are already in order.
You still need to call srand(time(NULL)) to initialize random
Topic archived. No new replies allowed.