How to Store Randomly Generated String and Compare them
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 UTC
Dec 24, 2018 at 5:36pm UTC
Your question is unclear.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
char rnd_alnum(int x) {
if (x < 0) return '?' ;
if (x < 10) return x + '0' ;
if (x < 36) return x - 10 + 'A' ;
if (x < 62) return x - 36 + 'a' ;
return '?' ;
}
int main() {
srand(time(0));
for (int row = 0; row < 7; ++row) {
for (int col = 0; col < 15; ++col)
cout << rnd_alnum(rand() % 62) << ' ' ;
cout << '\n' ;
}
}
r 6 M e V A Y 0 S g W Q f r M
S Q 3 e U K e u Q N L 6 z P U
A E Y W r 3 h P 3 7 3 Y Y i P
s 8 q t m I D Q B b m W f l t
7 t 8 f Q x i 5 M k C P I i 5
h Y D V R x m d M x C 8 R s r
K x l Q d B N J E h 1 Q 4 J 7
Dec 24, 2018 at 6:00pm UTC
How do I store a randomly generated string and Compare them
When you say "a randomly generated string", you're referring to a singular string.
When you say "compare", you implying at two strings.
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 32 33 34
#include <cstdlib>
#include <iostream>
#include <string>
#include <ctime>
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()
{ std::string a;
std::string b;
char c;
//seed every run
srand(time(0));
//
for (int x = 0; x < 7; ++x)
{ a += alphanumeric(rand() % 62);
b += alphanumeric(rand() % 62);
}
if (a == b)
cout << "strings are equal" << endl;
return 0;
}
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 UTC
Dec 28, 2018 at 6:29am UTC
Hey, thanks for the replies but fortunately I've already figured it out.
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 32 33 34 35 36 37 38 39 40 41 42 43 44
#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());
//
int x,i;
int account;
string pass;
string passholder[11];
for (x = 0; x < 11; ++x){
for (i = 0; i < 10; ++i){
pass = alphanumeric(rand()%62);
passholder[x] += pass;
}
cout << x << "--------" << passholder[x]<<endl;
passholder[x] += '\0' ; //prevent concatation
}
cout << "Element [0] has " << passholder[0] << endl;
cout << "Choose an Account " ;
cin >> account;
if (account > 11){
cout << "Account Not found" ;
}
else {
cout << "Account Found!" ;
}
return 0;
}
0--------hz4GFdiMyr
1--------9o2DQ1T9zY
2--------axBDiliEjv
3--------VyS41tnzfn
4--------qjDIcpYFC5
5--------OnsXvCntkk
6--------YJjuRsBA47
7--------CY8CqAN4gq
8--------q4ri1AKERJ
9--------yG3Nfdvfv7
10--------BRmkWP9eG2
Element [0] has hz4GFdiMyr
Topic archived. No new replies allowed.