Question

In: Computer Science

Need a c++ program to generate a random string of letters between 8 and 16 characters.

Need a c++ program to generate a random string of letters between 8 and 16 characters.

Solutions

Expert Solution

C++ code for the given problem statement:

#include <bits/stdc++.h> 
using namespace std; 
  
const int MAX = 26; 
  
// Returns a string of random alphabets of 
// length between 8 to 16 characters
string printRandomString() 
{   int num = (rand() % (16 - 8 + 1)) + 8; 
    char alphabet[MAX] = { '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' }; 
  
    string res = ""; 
    for (int i = 0; i < num; i++)  
        res = res + alphabet[rand() % MAX]; 
      
    return res; 
} 
  
// Driver code 
int main() 
{ 
   srand(time(NULL)); 
   cout << printRandomString(); 
   return 0; 
} 

Editor Screenshot:

Sample Output:

I hope you find the solution helpful.

Keep Learning!


Related Solutions

C++ program Generate and display two random characters involving upper and lowercase
C++ program Generate and display two random characters involving upper and lowercase
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
In C++, write a program that will read up to 10 letters (characters) into an array...
In C++, write a program that will read up to 10 letters (characters) into an array and write the letters back to the screen in the reverse order. The program should prompt the user to input all values and can determine whether the input has ended by a punctuation mark such as ‘.’ (make sure that you’ll include meaningful and intuitive messages to the user). For example, if the input is abcde, the output should be edcba. Include appropriate messaging...
Write a C program with a struct that contains a string of 12 characters and two...
Write a C program with a struct that contains a string of 12 characters and two integers: number1 and number2. Read all 3 values in from the keyboard using scanf. Print the string and the sum of the two integers using the fully qualified struct names.
Write a method that computes the number of lowercase letters (a-z) characters in a String: The...
Write a method that computes the number of lowercase letters (a-z) characters in a String: The method signature is as follows:  public int count(String s)
Suppose that a computer program randomly generates an 8-letter string from the letters A,B,C,D,E. For example,...
Suppose that a computer program randomly generates an 8-letter string from the letters A,B,C,D,E. For example, the program might generate the string CCCCCCCC or DAAEDCBB. The letter in each of the 8 positions is chosen independently of the other positions, and each of the five letters is chosen with equal likelihood. What is the probability that the string contains at least one A or at least one B?
In c++, using stack structure, write a program that will take a sequence of characters (string)...
In c++, using stack structure, write a program that will take a sequence of characters (string) and determine whether it is a palindrome. Use the linked version of the stack.
write a simple c program to reverse pairs of characters in a string. For exaple, "notified"...
write a simple c program to reverse pairs of characters in a string. For exaple, "notified" becomes "edfitino". The reversal should be in this order in a simple easy to understand c program.
Write a C++ program which reads a string, less than 10 characters long. This string represents...
Write a C++ program which reads a string, less than 10 characters long. This string represents an integer expressed in roman numbers. Let a function convert the number from roman to arabic form (i.e., our standard digits). Let then the main program writes out both forms. The roman numbers are written according to: M = 1000, D = 500, C =100, L=50, X=10, V=5, I=1. Examples: LXXXVII = 87 CCXIX = 219 MCCCLIV = 1354 MMDCLXXIII = 2673
I need a C++ program using while loops that counts the number of characters in a...
I need a C++ program using while loops that counts the number of characters in a sentence. The user inputs a sentence and then terminates the input with either '.' or '!'. And then it needs to count and display the number of a's, e's, i's, o's, u's, and consonants. The program should read both lower and upper case. They don't want us using switch statements or string operators, and want us to us if else if statements. I have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT