-1

Write a program which generate random words (number of words = n). Max length of word = m. Words must contain big and samll letters. Probability of big letters must eqals 50%.

Example:

  1. Input: 2 4
  2. Output: AbCd eFgH

How do I do that?

So far i figured out how to generate random small and big letter.

My code:

#include <iostream>

using namespace std;

int main()
{

   int n,m,s;
   cin >> n;
   cin >> m;
   s=n*m;
   char Tab[s];

   for(int i=0; i<n*m; i++)
   {
       Tab[i]= 'A' + rand()%25;
   }
    
   for(int i=1; i<n*m; i++)
   {
       Tab[i+2]= 'a' + rand()%25;
   }
    
   for(int i=0; i<n*m; i++)
   {
       cout << Tab[i] << " ";
   }

    return 0;
}
JaredPL
  • 25
  • 1
  • 5

1 Answers1

0

Code-

#include <iostream>
#include <time.h>
#include <string>
#include <stdlib.h>
using namespace std;

int main() {
    int n,m;
    cin>>n>>m;
    srand(time(NULL));//  without this rand() function might continuously give the same value
    
    while(n--){
        int stringLen = (rand() % m) +1; // getting random length
        string s=""; // taking null string
        for(int i=0; i<stringLen; i++){
            if(rand() % 2 == 0 ){ // capital or small letter
                s += 'A' + (rand() % 26);
            }else{
                s += 'a' + (rand() % 26);
            }
        }
        cout<<s<<" ";
    }
}
Mahedi Kamal
  • 179
  • 1
  • 9
  • 1
    You might want to read [Why should I not #include ](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Especially the combination of both can be quite dangerous and replacing it would improve your answer greatly. – Lukas-T Feb 14 '21 at 15:16