Q:

C++ program to generate random alphabets and store them into a character array

0

C++ program to generate random alphabets and store them into a character array

Random is one of the most cumbersome concepts to understand in C++. Multiple functions with same name are sort of confusing even for experienced programmers. However, applying them correctly can be fun. Here is a simple program that will print the alphabets in a random manner.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the program:

#include <iostream>
using namespace std;

#include <stdlib.h>
#include <time.h>

int main()
{	
	char alphabets[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
	char rString[20];

	srand(time(NULL));

	int i=0;
	while(i<20) {
		int temp = rand() % 26;
		rString[i] = alphabets[temp];
		i++;
	}

	for(i=0; i<20; i++)
		cout<<rString[i];

	return 0;
}

Output

qaicgosutvmscsfufpfk

In this program first we take two character arrays, one to store all the alphabets and other to print random characters into it. To keep it simple we have taken the length as 20.

Now we initialize the seed to current system time so that every time a new random seed is generated.

Next, in the while loop, we loop through the length of the random string (20 here) and store random generated alphabets. We take a temp variable to generate a number between 0-26. Then we provide this number to alphabets array and a random string is generated.

After that we print the final random generated string.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Most popular and Searched C++ solved programs with Explanation and Output

Similar questions


need a help?


find thousands of online teachers now
C++ program to find factorial of large numbers usi... >>
<< C++ program to print pattern of stars till N numbe...