

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I create a random alpha-numeric string using C++?
In this section we will see how to generate a random alphanumeric string using C++. Here we are providing lowercase letters, uppercase letters and numbers (0-9). This program takes the characters randomly, then creates the random string.
Input: Here we are giving the string length Output: A random string of that length. Example “XSme6VAsvJ”
Algorithm
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
Example Code
#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; }
Output
Enter string length: 10 XSme6VAsvJ
- Related Questions & Answers
- How do I create a random four-digit number in MySQL?
- How do I create a popup window using Tkinter?
- How do I create a popup window using Tkinter Program?
- How do I generate random floats in C++?
- How do I create a Python namespace?
- How do I create a Java string from the contents of a file?
- How do I format a string using a dictionary in Python 3?
- How do I create a view in MySQL?
- How can I parse a numeric string to its corresponding float value?
- How do I create an automatically updating GUI using Tkinter?
- How do I do a case insensitive string comparison in Python?
- How do I create a namespace package in Python?
- How do I create a date picker in tkinter?
- How do I create a java.sql.Date object in Java?
- How do I create a popup window in Tkinter?
Advertisements