// how to answer this question in C++ with the following ? //Write a function th
ID: 674084 • Letter: #
Question
// how to answer this question in C++ with the following ?
//Write a function that generates a string of random letters of a given length. The function's prototype is
//string randomword(int length);
//where n is the string's length. The function makes sure that its parameter is a non-negative integer.
//Write program to test the function.
#include <iostream>
#include <random>
#include <string>
#include <time.h>
using namespace std;
const int nalphas = 26;
const string emptystring = "";
default_random_engine e((unsigned)time(0));
uniform_int_distribution<int> u(0, nalphas - 1);
string randomword(int length);
int main()
{
for (int counter = 1; counter <= 64; counter++)
cout << randomword(4) << endl;
system("pause");
return 0;
}
string randomword(int length)
{
string rstring = emptystring;
return rstring;
}
Explanation / Answer
In C++:
void gen_random(char *s, const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
s[len] = 0;
}
This is C code but this will make your answer I hope
static char *rand_string(char *str, size_t size)
{
const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJK...";
if (size) {
--size;
for (size_t n = 0; n < size; n++) {
int key = rand() % (int) (sizeof charset - 1);
str[n] = charset[key];
}
str[size] = '';
}
return str;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.