School-Coding-Cpp/sfusi/esercizio2_stringhe_concate...

58 lines
1.5 KiB
C++

/*
Nome: Mario
Cognome: Montanari
Concatenare le parole casuali in una stringa frase:
- separate da un numero casuale di spazi compreso tra 1 e 3.
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
#define MIN_SILLABE 2
#define MAX_SILLABE 3
#define MIN_SPAZI 1
#define MAX_SPAZI 3
using namespace std;
void creaParola(const char strVowel[], const char strConsonant[], const char strSpazio[], int sillabe, int spazi);
int main(void) {
srand(time(NULL));
const char strVowel[] = "AEIOU";
const char strConsonant[] = "BCDFGLMNPRSTVZ";
const char strSpazio[] = " ";
int sillabe = rand() % (MAX_SILLABE - MIN_SILLABE + 1) + MIN_SILLABE;
int spazi = rand() % (MAX_SPAZI - MIN_SPAZI + 1) + MIN_SPAZI;
cout << "Numero di spazi: " << spazi << endl;
creaParola(strVowel, strConsonant, strSpazio, sillabe, spazi);
return 0;
}
void creaParola(const char strVowel[], const char strConsonant[], const char strSpazio[], int sillabe, int spazi) {
int size = (sillabe * 2 * (spazi + 1)) + spazi + 1;
char strParola[size];
int index = 0;
for (int i = 0; i < spazi + 1; i++) {
for (int j = 0; j < sillabe; j++) {
strParola[index++] = strConsonant[rand() % 14];
strParola[index++] = strVowel[rand() % 5];
}
if (i < spazi) {
sillabe = rand() % (MAX_SILLABE - MIN_SILLABE + 1) + MIN_SILLABE;
strParola[index++] = strSpazio[0];
}
}
strParola[index] = '\0';
cout << "Frase generata: " << strParola << endl;
}