Dec 24, 2018 at 9:42am Dec 24, 2018 at 9:42am UTC
Hello, How do I store a randomly generated string and Compare them
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include "main.h" //all the includes
using namespace std;
//generate strings and numbers
char alphanumeric(int x){
if (x < 10)
return char (x+48);
if (x < 36)
return char (x+55);
if (x < 62)
return char (x+61);
return '?' ;
}
int main()
{
//seed every run
srand(time(0));
srand(rand());
//
for (int x = 0; x < 7; ++x){
for (int i = 0; i < 5; ++i)
cout << alphanumeric(rand()%62);
cout<<endl;
}
return 0;
}
Last edited on Dec 24, 2018 at 11:25am Dec 24, 2018 at 11:25am UTC
Dec 24, 2018 at 6:28pm Dec 24, 2018 at 6:28pm UTC
He also mentions "storing" the strings. So perhaps something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <iostream>
#include <string>
#include <vector>
#include <random>
using namespace std;
const int NumStrings = 10;
const int StringLength = 15;
char rnd_alnum() {
static default_random_engine rnd(random_device{}());
static uniform_int_distribution<> dist(0, 61);
int x = dist(rnd);
if (x < 10) return x + '0' ;
if (x < 36) return x - 10 + 'A' ;
return x - 36 + 'a' ;
}
int main() {
vector<string> strs;
for (int i = 0; i < NumStrings; ++i) {
string str;
for (int j = 0; j < StringLength; ++j)
str += rnd_alnum();
strs.push_back(str);
}
for (const auto & str: strs)
cout << str << '\n' ;
}
As for "comparing" the strings, that could mean a few things. Maybe he wants to know how many characters they have in common ... who knows?
Last edited on Dec 24, 2018 at 6:31pm Dec 24, 2018 at 6:31pm UTC