A PHP Error was encountered

Severity: Warning

Message: fopen(/var/cpanel/php/sessions/ea-php56/PHPSESSIDn38e9bduc6q7uq23u3fuau1ht282j3sv): failed to open stream: No space left on device

Filename: drivers/Session_files_driver.php

Line Number: 174

C++ program to generate random alphabets and store them into a character array
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...

A PHP Error was encountered

Severity: Warning

Message: Unknown: Failed to write session data (user). Please verify that the current setting of session.save_path is correct (/var/cpanel/php/sessions/ea-php56)

Filename: Unknown

Line Number: 0