forked from mrigaankzoro/Hacktoberfest24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateRandomString.cpp
44 lines (34 loc) · 1.06 KB
/
GenerateRandomString.cpp
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 <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
string generateRandomString(int length) {
const string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789";
string randomString;
// Generate random characters
for (int i = 0; i < length; ++i) {
int index = rand() % characters.size();
randomString += characters[index];
}
return randomString;
}
int main() {
int length;
// Seed the random number generator
srand(static_cast<unsigned int>(time(0)));
// Take input for the length of the random string
cout << "Enter the length of the random string: ";
cin >> length;
// Check for valid input
if (length <= 0) {
cout << "Length should be a positive integer." << endl;
return 1;
}
// Generate and display the random string
string randomString = generateRandomString(length);
cout << "Random String: " << randomString << endl;
return 0;
}