इस खंड में हम देखेंगे कि C++ का उपयोग करके एक यादृच्छिक अल्फ़ान्यूमेरिक स्ट्रिंग कैसे उत्पन्न करें। यहां हम लोअरकेस लेटर्स, अपरकेस लेटर्स और नंबर (0-9) प्रदान कर रहे हैं। यह प्रोग्राम वर्णों को बेतरतीब ढंग से लेता है, फिर यादृच्छिक स्ट्रिंग बनाता है।
Input: Here we are giving the string length Output: A random string of that length. Example “XSme6VAsvJ”
एल्गोरिदम
Step 1:Define array to hold all uppercase, lowercase letters and numbers Step 2: Take length n from user Step 3: Randomly choose characters’ n times and create a string of length n Step 4: End
उदाहरण कोड
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
static const char alphanum[] = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz";
int len = sizeof(alphanum) - 1;
char genRandom() { // Random string generator function.
return alphanum[rand() % len];
}
int main() {
srand(time(0));
int n;
cout << "Enter string length: ";
cin >> n;
for(int z = 0; z < n; z++) { //generate string of length n
cout << genRandom(); //get random character from the given list
}
return 0;
} आउटपुट
Enter string length: 10 XSme6VAsvJ