diff --git a/sfusi/acronym.cpp b/sfusi/acronym.cpp new file mode 100644 index 0000000..573967d --- /dev/null +++ b/sfusi/acronym.cpp @@ -0,0 +1,35 @@ +/* +Nome: Mario +Cognome: Montanari +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *acronym(char *const str); + +int main(void) { + char str[SIZE] = "Organizzazione nazioni unite"; + + cout << str << endl; + cout << acronym(str) << endl; + + return 0; +} + +char *acronym(char *const str) { + int w = 0; + for (int i = 0; str[i] != '\0'; i++) { + if (isalpha(str[i]) && (i == 0 || isspace(str[i - 1]))) { + str[w] = toupper(str[i]); + w++; + } + } + str[w] = '\0'; + + return str; +} \ No newline at end of file diff --git a/sfusi/alfabeto_carbonaro.cpp b/sfusi/alfabeto_carbonaro.cpp new file mode 100644 index 0000000..6400e7d --- /dev/null +++ b/sfusi/alfabeto_carbonaro.cpp @@ -0,0 +1,43 @@ +/* +Nome: Mario +Cognome: Montanari + +Convertire una stringa in alfabeto carbonaro. +*/ + +#include +#include +#include + +#define SIZE 100+1 + +using namespace std; + +void alfCarbonaro(char mssg[], const char alfCarb[]); + +int main(void) { + const char alfCarb[] = "OPGTIVCHE RNMABQLZDUF S"; + char mssg[SIZE]; // ABCDEFGHI LMNOPQRSTUV Z + + cout << "Inserisci una parola: "; + cin >> mssg; + + alfCarbonaro(mssg, alfCarb); + + return 0; +} + +void alfCarbonaro(char mssg[], const char alfCarb[]) { + int r; + for (int i = 0; mssg[i] != '\0'; i++) { + r = toupper(mssg[i]) - 'A'; + mssg[i] = alfCarb[r]; + + if (alfCarb[r] == ' ') { + cout << "Parola non valida!" << endl; + return; + } + } + + cout << mssg; +} \ No newline at end of file diff --git a/sfusi/camelCase.cpp b/sfusi/camelCase.cpp new file mode 100644 index 0000000..6dd5d42 --- /dev/null +++ b/sfusi/camelCase.cpp @@ -0,0 +1,49 @@ +/* +Nome: Mario +Cognome: Montanari + +camelCase +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *camelCase(char *const str); + +int main(void) { + char str[SIZE]; + + cout << "Inserisci una frase: "; + cin.getline(str, SIZE); + + camelCase(str); + + cout << "Frase aggiornata: " << str << endl; + + return 0; +} + +char *camelCase(char *const str) { + if (str[0] == '\0') { + return str; + } + + str[0] = toupper(str[0]); + + + int i = 1; + while (str[i] != '\0') { + if (isalpha(str[i]) && isblank(str[i - 1])) { + str[i] = toupper(str[i]); + } else { + str[i] = tolower(str[i]); + } + i++; + } + + return str; +} \ No newline at end of file diff --git a/sfusi/contaParole.cpp b/sfusi/contaParole.cpp new file mode 100644 index 0000000..c974088 --- /dev/null +++ b/sfusi/contaParole.cpp @@ -0,0 +1,40 @@ +/* +Nome: Mario +Cognome: Montanari + +contaParole +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +void contaParole(const char *str); + +int main(void) { + char str[SIZE]; + + cout << "Inserisci una frase: "; + cin.getline(str, SIZE); + + contaParole(str); + + return 0; +} + +void contaParole(const char *str) { + int count = 0; + char oldChr = ' '; + + for (int i = 0; str[i] != '\0'; i++) { + if (isspace(oldChr) && !isspace(str[i])) { + count++; + } + oldChr = str[i]; + } + + cout << count; +} \ No newline at end of file diff --git a/sfusi/delDigit.cpp b/sfusi/delDigit.cpp new file mode 100644 index 0000000..50a6025 --- /dev/null +++ b/sfusi/delDigit.cpp @@ -0,0 +1,41 @@ +/* +Nome: Mario +Cognome: Montanari + +delDigit (come removeDigit) +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *delDigit(char *const str); + +int main(void) { + char str[SIZE] = "H3ll0 W0rld! 2025"; + + cout << "Before: " << str << endl; + + delDigit(str); + + cout << "After: " << str << endl; + + return 0; +} + +char *delDigit(char *const str) { + char *dst = str; + + for (char *src = str; *src != '\0'; src++) { + if (!isdigit(*src)) { + *dst++ = *src; + } + } + + *dst = '\0'; + + return str; +} \ No newline at end of file diff --git a/sfusi/delEvenDigits_delOddDigits.cpp b/sfusi/delEvenDigits_delOddDigits.cpp new file mode 100644 index 0000000..4cf2897 --- /dev/null +++ b/sfusi/delEvenDigits_delOddDigits.cpp @@ -0,0 +1,89 @@ +/* +Nome: Mario +Cognome: Montanari + +delEvenDigits & delOddDigits +*/ + +#include +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *delEvenDigits(char *const src); +char *delOddDigits(char *const dst); + +int main(void) { + char src[SIZE] = "25dic2018"; + char dst[SIZE]; + + strcpy(dst, src); + + cout << "Original string: " << setw(10) << src << endl; + + delEvenDigits(src); + cout << "delEvenDigit: " << setw(9) << src << endl; + + delOddDigits(dst); + cout << "delOddDigit: " << setw(12) << dst << endl; + + return 0; +} + +// Con puntatori +char *delEvenDigits(char *const src) { + char *w = src; + for (char *r = src; *r != '\0'; r++) { + if (!isdigit(*r) || (*r - '0') % 2 != 0) { + *w++ = *r; + } + } + *w = '\0'; + + return src; +} + +/* Senza puntatori +char *delEvenDigits(char *const src) { + int w = 0; + for (int r = 0; src[r] != '\0'; r++) { + if (!isdigit(src[r]) || (src[r] - '0') % 2 != 0) { + src[w++] = src[r]; + } + } + src[w] = '\0'; + + return src; +} +*/ + +// Con puntatori +char *delOddDigits(char *const dst) { + char *w = dst; + for (char *r = dst; *r != '\0'; r++) { + if (!isdigit(*r) || (*r - '0') % 2 == 0) { + *w++ = *r; + } + } + *w = '\0'; + + return dst; +} + +/* Senza puntatori +char *delOddDigits(char *const dst) { + int w = 0; + for (int r = 0; dst[r] != '\0'; r++) { + if (!isdigit(dst[r]) || (dst[r] - '0') % 2 == 0) { + // Copia + dst[w++] = dst[r]; + } + } + dst[w] = '\0'; + + return dst; +} +*/ \ No newline at end of file diff --git a/sfusi/dupDigit.cpp b/sfusi/dupDigit.cpp new file mode 100644 index 0000000..3af32d4 --- /dev/null +++ b/sfusi/dupDigit.cpp @@ -0,0 +1,53 @@ +/* +Nome: Mario +Cognome: Montanari + +dupDigit +*/ + +#include +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *dupDigit(char *const str); + +int main(void) { + char str[SIZE] = "H3ll0 W0rld! 2025"; + + cout << "Before: " << str << endl; + + dupDigit(str); + + cout << "After: " << str << endl; + + return 0; +} + +// Con puntatori +char *dupDigit(char *const str) { + for (char *r = str; *r != '\0'; r++) { + if (isdigit(*r)) { + memmove(r + 1, r, strlen(r) + 1); + r = r + 1; + } + } + + return str; +} + +/* Senza puntatori +char *dupDigit(char *const str) { + for (int i = 0; str[i] != '\0'; i++) { + if (isdigit(str[i])) { + memmove(&str[i + 1], &str[i], strlen(&str[i]) + 1); + i = i + 1; + } + } + + return str; +} +*/ \ No newline at end of file diff --git a/sfusi/dupVowels_dupConsonants.cpp b/sfusi/dupVowels_dupConsonants.cpp new file mode 100644 index 0000000..e342b2d --- /dev/null +++ b/sfusi/dupVowels_dupConsonants.cpp @@ -0,0 +1,96 @@ +/* +Nome: Mario +Cognome: Montanari + +dupVowels & dupConsonants +*/ + +#include +#include +#include +#include + +#define SIZE 100+1 + +using namespace std; + +bool isVowel(char c); +bool isConsonant(char c); +char *dupVowels(char *const src); +char *dupConsonants(char *const str); + +int main(void) { + char src[SIZE] = "12 ottobre 1492"; + char dst[SIZE]; + + strcpy(dst, src); + + cout << "Original string: " << setw(16) << src << endl; + + dupVowels(src); + cout << "dupVowels: " << setw(25) << src << endl; + + dupConsonants(dst); + cout << "dupConsonants: " << setw(22) << dst << endl; + + return 0; +} + +bool isVowel(char c) { + return strchr("aeiou", tolower(c)); +} + +bool isConsonant(char c) { + return strchr("bcdfghjklmnpqrstvwxyz", tolower(c)); +} + +// Con puntatori +char *dupVowels(char *const src) { + for (int r = 0; src[r] != '\0'; r++) { + if (isVowel(src[r])) { + memmove(src + r + 1, src + r, strlen(src + r) + 1); + r++; + } + } + + return src; +} + +/* Senza puntatori +char *dupVowels(char *const src) { + for (int r = 0; src[r] != '\0'; r++) { + if (isVowel(src[r])) { + memmove(src + r + 1, src + r, strlen(src + r) + 1); + r++; + } + } + + return src; +} +*/ + + +// Con puntatori +char *dupConsonants(char *const dst) { + for (int r = 0; dst[r] != '\0'; r++) { + if (isConsonant(dst[r])) { + memmove(dst + r + 1, dst + r, strlen(dst + r) + 1); + r++; + } + } + + return dst; +} + +/* Senza puntatori +char *dupConsonants(char *const dst) { + for (int r = 0; dst[r] != '\0'; r++) { + if (isConsonant(dst[r])) { + memmove(dst + r + 1, dst + r, strlen(dst + r) + 1); + r++; + } + } + + return dst; +} +*/ \ No newline at end of file diff --git a/sfusi/esercizio10_stringhe.cpp b/sfusi/esercizio10_stringhe.cpp new file mode 100644 index 0000000..e8079dd --- /dev/null +++ b/sfusi/esercizio10_stringhe.cpp @@ -0,0 +1,53 @@ +/* +Nome: Mario +Cognome: Montanari +*/ + +#include + +#define SIZE 100+1 + +using namespace std; + +int isAlpha(char c); +int isNum(int c); + +int main(void) { + char src[SIZE]; + + cin.getline(src, SIZE); + + if (strlen(src) != 16) { + cout << "Codice fiscale incorretto!" << endl; + } + + bool codiceFiscale = true; + + for (int i = 0; i < 16; i++) { + if ((i < 6 && !isAlpha(src[i])) || + (i >= 6 && i < 8 && !isNum(src[i])) || + (i == 8 && !isAlpha(src[i])) || + (i >= 9 && i < 11 && !isNum(src[i])) || + (i == 11 && !isAlpha(src[i])) || + (i >= 12 && i < 15 && !isNum(src[i])) || + (i == 15 && !isAlpha(src[i]))) { + codiceFiscale = false; + } + } + + if (codiceFiscale) { + cout << "Codice fiscale corretto!" << endl; + } else { + cout << "Codice fiscale incorretto!" << endl; + } + + return 0; +} + +int isAlpha(char c) { + return (c >= 'A' && c<= 'Z') || (c >= 'a' && c<= 'z'); +} + +int isNum(int c) { + return (c >= '0' && c <= '9'); +} \ No newline at end of file diff --git a/sfusi/esercizio1_stringhe.cpp b/sfusi/esercizio1_stringhe.cpp new file mode 100644 index 0000000..abcb486 --- /dev/null +++ b/sfusi/esercizio1_stringhe.cpp @@ -0,0 +1,49 @@ +/* +Nome: Mario +Cognome: Montanari + +Generare una parola a caso (sicuramente pronunciabile), esempio: MUSETO. +Consonanti = "BCDFGLMNPRSTVZ". +Vocali = "AEIOU". +Ogni sillaba è consonante + vocale, esempio: BA. +Il numero di sillabe viene deciso da input o in modo randomico. +*/ + +#include +#include +#include + +#define MIN_SILLABE 2 +#define MAX_SILLABE 5 + +using namespace std; + +void creaParola(const char strVowel[], const char strConsonant[], int n); + +int main(void) { + srand(time(NULL)); + + const char strVowel[] = "AEIOU"; + const char strConsonant[] = "BCDFGLMNPRSTVZ"; + + int n = rand() % (MAX_SILLABE - MIN_SILLABE + 1) + MIN_SILLABE; + cout << "Numero di sillabe: " << n << endl; + + creaParola(strVowel, strConsonant, n); + + return 0; +} + +void creaParola(const char strVowel[], const char strConsonant[], int n) { + int size = (n * 2) + 1; + char strParola[size]; + int index = 0; + + for (int i = 0; i < n; i++) { + strParola[index++] = strConsonant[rand() % 14]; + strParola[index++] = strVowel[rand() % 5]; + } + + strParola[index] = '\0'; + cout << "Parola generata: " << strParola << endl; +} \ No newline at end of file diff --git a/sfusi/esercizio1_stringhe_generaParolePronunciabili.cpp b/sfusi/esercizio1_stringhe_generaParolePronunciabili.cpp new file mode 100644 index 0000000..5a828c5 --- /dev/null +++ b/sfusi/esercizio1_stringhe_generaParolePronunciabili.cpp @@ -0,0 +1,56 @@ +/* +Nome: Mario +Cognome: Montanari + +Generare una parola a caso (sicuramente pronunciabile), esempio: MUSETO. +Consonanti = "BCDFGLMNPRSTVZ". +Vocali = "AEIOU". +Ogni sillaba è consonante + vocale, esempio: BA. +Il numero di sillabe viene deciso da input o in modo randomico. +*/ + +#include +#include +#include +#include +#include +#define N_SILL 4 + +using namespace std; + +void generaParola(int numSillabe, char* parola, int maxLen); + +int main() { + const int MAX_LEN = 20; + char parolaGenerata[MAX_LEN]; + + srand((unsigned)time(NULL)); + int numSillabe = rand() % 3 + 2; + + generaParola(numSillabe, parolaGenerata, MAX_LEN); + + printf("Parola generata: %s\n", parolaGenerata); + + return 0; +} + +void generaParola(int numSillabe, char* parola, int maxLen) { + const char consonanti[] = "BCDFGLMNPRSTVZ"; + const char vocali[] = "AEIOU"; + + parola[0] = '\0'; + + int currentLen = 0; + + for (int i = 0; i < numSillabe; i++) { + if (currentLen + 2 >= maxLen) break; + + char sillaba[3]; + sillaba[0] = consonanti[rand() % (sizeof(consonanti) - 1)]; + sillaba[1] = vocali[rand() % (sizeof(vocali) - 1)]; + sillaba[2] = '\0'; + + strcat(parola, sillaba); + currentLen += 2; + } +} diff --git a/sfusi/esercizio20_stringhe.cpp b/sfusi/esercizio20_stringhe.cpp new file mode 100644 index 0000000..bab4625 --- /dev/null +++ b/sfusi/esercizio20_stringhe.cpp @@ -0,0 +1,54 @@ +/* +Nome: Mario +Cognome: Montanari +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +void codificaMessaggio(char dst[], const char alfDecod[], const char alfCod[]); +char *strcpy(char * const dst, const char *src); + +int main(void) { + char src[SIZE]; + char dst[SIZE]; + const char alfDecod[] = "AaBbCcDdEeFfGgHhIiLlMmNnOoPpQqRrSsTtUuVvZz"; + const char alfCod[] = "OoPpGgTtIiVvCcHhEeRrNnMmAaBbQqLlZzDdUuFfSs"; + + cout << ""; + cin.getline(src, SIZE); + + *strcpy(dst, src); + + codificaMessaggio(dst, alfDecod, alfCod); + + cout << "" << dst << endl; + + return 0; +} + +void codificaMessaggio(char dst[], const char alfDecod[], const char alfCod[]) { + for (int i = 0; i < strlen(dst); i++) { + if ((dst[i] >= 'A' && dst[i] <= 'Z') || (dst[i] >= 'a' && dst[i] <= 'z')) { + for (int j = 0; j < strlen(alfCod); j++) { + if (dst[i] == alfDecod[j]) { + dst[i] = alfCod[j]; + break; + } + } + } + } +} + +char *strcpy(char * const dst, const char *src) { + size_t i; + for (i = 0; src[i] != '\0'; i++) { + dst[i] = src[i]; + } + dst[i] = '\0'; + return dst; +} \ No newline at end of file diff --git a/sfusi/esercizio2_stringhe_concatenaParoleCasuali.cpp b/sfusi/esercizio2_stringhe_concatenaParoleCasuali.cpp new file mode 100644 index 0000000..15b4359 --- /dev/null +++ b/sfusi/esercizio2_stringhe_concatenaParoleCasuali.cpp @@ -0,0 +1,58 @@ +/* +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 +#include +#include + +#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; +} \ No newline at end of file diff --git a/sfusi/esercizio6_stringhe_generaPassword.cpp b/sfusi/esercizio6_stringhe_generaPassword.cpp new file mode 100644 index 0000000..1ae2f69 --- /dev/null +++ b/sfusi/esercizio6_stringhe_generaPassword.cpp @@ -0,0 +1,72 @@ +/* +Nome: Mario +Cognome: Montanari + +Genarare una password che soddisfa i criteri: + - Presenza di almeno una: + - minuscola; + - maiuscola; + - cifra; + - punteggiatura; + - Lunghezza della password compresa fra 8 e 12 caratteri; +*/ + +#include +#include +#include + +#define SIZE 12 + +#define NSTAMPA_MIN 5 +#define NSTAMPA_MAX 6 + +#define CARATTERI_MIN 2 +#define CARATTERI_MAX 3 + +#define MINUSCOLA_MIN 97 +#define MINUSCOLA_MAX 122 + +#define MAIUSCOLA_MIN 65 +#define MAIUSCOLA_MAX 90 + +#define CIFRA_MIN 48 +#define CIFRA_MAX 57 + +#define PUNTEGGIATURA_MIN 33 +#define PUNTEGGIATURA_MAX 47 + +using namespace std; + +void generaPassword(int size, char nMinuscola[], char nMaiuscola[], char nCifra[], char nPunteggiatura[], char nCaratteri); + +int main(void) { + srand(time(NULL)); + + char nMinuscola[SIZE]; + char nMaiuscola[SIZE]; + char nCifra[SIZE]; + char nPunteggiatura[SIZE]; + char nCaratteri; + + generaPassword(SIZE, nMinuscola, nMaiuscola, nCifra, nPunteggiatura, nCaratteri); + + return 0; +} + +void generaPassword(int size, char nMinuscola[], char nMaiuscola[], char nCifra[], char nPunteggiatura[], char nCaratteri) { + int nStampa = rand() % (NSTAMPA_MAX - NSTAMPA_MIN + 1) + NSTAMPA_MIN; + for (int j = 0; j < nStampa; j++) { + nCaratteri = rand() % (CARATTERI_MAX - CARATTERI_MIN + 1) + CARATTERI_MIN; + for (int i = 0; i < nCaratteri; i++) { + nMinuscola[i] = rand() % (MINUSCOLA_MAX - MINUSCOLA_MIN + 1) + MINUSCOLA_MIN; + cout << nMinuscola[i]; + nMaiuscola[i] = rand() % (MAIUSCOLA_MAX - MAIUSCOLA_MIN + 1) + MAIUSCOLA_MIN; + cout << nMaiuscola[i]; + nCifra[i] = rand() % (CIFRA_MAX - CIFRA_MIN + 1) + CIFRA_MIN; + cout << nCifra[i]; + nPunteggiatura[i] = rand() % (PUNTEGGIATURA_MAX - PUNTEGGIATURA_MIN + 1) + PUNTEGGIATURA_MIN; + cout << nPunteggiatura[i]; + } + cout << endl; + } +} \ No newline at end of file diff --git a/sfusi/esercizio7_stringhe_stampaCodiceAsciiCorrispondente.cpp b/sfusi/esercizio7_stringhe_stampaCodiceAsciiCorrispondente.cpp new file mode 100644 index 0000000..a4c6be5 --- /dev/null +++ b/sfusi/esercizio7_stringhe_stampaCodiceAsciiCorrispondente.cpp @@ -0,0 +1,37 @@ +/* +Nome: Mario +Cognome: Montanari + +Scrivere una funzione che stampi la stringa ricevuta +e anche la sequenza di codici ASCII corrispondenti. +*/ + +#include +#include +#include + +#define SIZE 100+1 + +using namespace std; + +void stampaAscii(char *src); + +int main(void) { + char src[SIZE]; + + cin.getline(src, SIZE); + + stampaAscii(src); + + return 0; +} + +void stampaAscii(char *src) { + char chr; + int asciiCode; + for (int i = 0; i < strlen(src); i++) { + chr = src[i]; + asciiCode = chr; + cout << chr << "(" << asciiCode << ")" << " "; + } +} \ No newline at end of file diff --git a/sfusi/esercizio8_stringhe_stampaTuttiCodiciAscii.cpp b/sfusi/esercizio8_stringhe_stampaTuttiCodiciAscii.cpp new file mode 100644 index 0000000..fa54089 --- /dev/null +++ b/sfusi/esercizio8_stringhe_stampaTuttiCodiciAscii.cpp @@ -0,0 +1,39 @@ +/* +Nome: Mario +Cognome: Montanari + +Scrivere una funzione che stampi tutti i codici ASCII: + - attenzione ai caratteri non stampabili; +*/ + +#include +#include + +#define SIZE 94 + +using namespace std; + +void stampaArrayChr(char *arrChr, int size); + +int main(void) { + char arrChr[SIZE]; + // int nChr = sizeof(arrChr) / sizeof(arrChr[0]); + + // iota(arrChr, arrChr + nChr, '!'); + + stampaArrayChr(arrChr, SIZE); + + return 0; +} + +void stampaArrayChr(char *arrChr, int size) { + for (int i = 0; i < SIZE; i++) { + arrChr[i] = '!'; + arrChr[i] = arrChr[i] + i; + } + + for (int i = 0; i < size; i++) { + cout << arrChr[i] << " "; + } + cout << endl; +} \ No newline at end of file diff --git a/sfusi/esercizio9_stringhe.cpp b/sfusi/esercizio9_stringhe.cpp new file mode 100644 index 0000000..572c8cf --- /dev/null +++ b/sfusi/esercizio9_stringhe.cpp @@ -0,0 +1,41 @@ +/* +Nome: Mario +Cognome: Montanari +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +int isLower(char c) { + return (c >= 'a' && c <= 'z'); +} + +int toUpper(char c) { + if (isLower(c)) { + return c - ('a' - 'A'); + } + return c; +} + +int main(void) { + char src[SIZE]; + + cin.getline(src, SIZE); + + bool nuovaParola = true; + + for (int i = 0; i < strlen(src); i++) { + if (nuovaParola && isLower(src[i])) { + src[i] = toUpper(src[i]); + } + nuovaParola = (src[i] == ' '); + } + + cout << src << endl; + + return 0; +} \ No newline at end of file diff --git a/sfusi/esercizioInterrogazione_Maruccia.cpp b/sfusi/esercizioInterrogazione_Maruccia.cpp new file mode 100644 index 0000000..97504ef --- /dev/null +++ b/sfusi/esercizioInterrogazione_Maruccia.cpp @@ -0,0 +1,76 @@ +/* +Nome: Mario +Cognome: Montanari +*/ + +#include +#include + +#define USO 20 +#define DIM 2000 +#define VMAX 'z' +#define VMIN 'a' + +using namespace std; + +typedef struct{ + char ch1; + char ch2; +}elemento; + +typedef struct{ + int CVU; //coppie vocali uguali + int CCU; //coppie consonanti uguali + int posizioni[DIM]; +}result; + +void initArray(elemento arr[], int size){ + for(int i= 0; i < size ; i++){ + arr[i].ch1 = rand()%(VMAX-VMIN+1)+VMIN; + arr[i].ch2 = rand()%(VMAX-VMIN+1)+VMIN; + } +} +void stampaArray(elemento arr[], int size){ + for(int i= 0; i < size ; i++){ + if(arr[i].ch1 == arr[i].ch2){ + cout << arr[i].ch1 << endl; + cout << arr[i].ch2 << endl; + cout << endl; + } + } +} + +result risultato(elemento arr[], int size){ + int index=0; + result risultato = {0,0,0}; + for(int i = 0; i +#include +#include + +#define SIZE 100 + +using namespace std; + +typedef struct { + char car; + int num; +} elemento; + +void init(elemento arr[], int size) { + for (int i = 0; i < size; i++) { + arr[i].car = 'A' + i; + arr[i].num = 0; + } +} + +void stampa(elemento arr[], int size) { + for (int i = 0; i < size; i++) { + cout << arr[i].car << ": " << arr[i].num << endl; + } +} + +void conta(elemento arr[], char str[], int size) { + + for (int i = 0; str[i] != '\0'; i++) { + int j = 0; + while (str[i] != arr[j].car) { + j++; + } + arr[j].num++; + } +} + +int main(void) { + elemento arr[SIZE]; + int lunghezzaAlfabeto = ('Z' - 'A') + 1; + char str[SIZE]; + + cout << "Inserisci una parola: "; + cin.getline(str, SIZE); + + for (int i = 0; str[i] != '\0'; i++) { + str[i] = toupper(str[i]); + } + + init(arr, lunghezzaAlfabeto); + conta(arr, str, lunghezzaAlfabeto); + stampa(arr, lunghezzaAlfabeto); + + return 0; +} \ No newline at end of file diff --git a/sfusi/esercizioInterrogazione_Nyerges.cpp b/sfusi/esercizioInterrogazione_Nyerges.cpp new file mode 100644 index 0000000..0906605 --- /dev/null +++ b/sfusi/esercizioInterrogazione_Nyerges.cpp @@ -0,0 +1,87 @@ +/* +Nome: Mario +Cognome: Montanari + +Contare il numero di vocali minuscole e +maiuscole contenute in una frase data in input. +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +typedef struct{ + int nVocMin; + int nVocMai; +} result; + +result vocali (char str[], int size); +bool isVocMin(char chr); +bool isVocMai(char chr); + +int main (void){ + char str[SIZE]; + result risultato; + + cout << "Inserisci una frase: "; + cin.getline(str, SIZE); + + risultato = vocali(str, sizeof(str)/sizeof(str[0])); + + cout << endl << "Numero di vocali minuscole: " << risultato.nVocMin << endl; + cout << "Numero di vocali maiuscole: " << risultato.nVocMai << endl; + + return 0; +} + +result vocali (char str[], int size){ + result res = {0, 0}; + + for(int i = 0; i < size; i++){ + + if(isVocMin(str[i])){ + res.nVocMin++; + } + + if(isVocMai(str[i])){ + res.nVocMai++; + } + } + + return res; +} + +bool isVocMin(char chr){ + bool isMin = false; + + if( + chr == 'a' || + chr == 'e' || + chr == 'i' || + chr == 'o' || + chr == 'u' + ){ + isMin = true; + } + + return isMin; +} + +bool isVocMai(char chr){ + bool isMai = false; + + if ( + chr == 'A' || + chr == 'E' || + chr == 'I' || + chr == 'O' || + chr == 'U' + ){ + isMai = true; + } + + return isMai; +} \ No newline at end of file diff --git a/sfusi/formattaData.cpp b/sfusi/formattaData.cpp new file mode 100644 index 0000000..d03b21f --- /dev/null +++ b/sfusi/formattaData.cpp @@ -0,0 +1,46 @@ +/* +Nome: Mario +Cognome: Montanari + +formattaData +*/ + +#include +#include +#include + +#define SIZE 100+1 +#define NUM_MESI 12 + +using namespace std; + +void formattaData(char *const data); + +int main(void) { + char data[SIZE]; + + cout << "Inserisci una data: "; + cin.getline(data, SIZE); + + formattaData(data); + + return 0; +} + +void formattaData(char *const data) { + const char *mesi[NUM_MESI] = {"gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"}; + int gg; + int mm; + int aaaa; + + sscanf(data, "%d/%d/%d", &gg, &mm, &aaaa); + + if (mm < 0 || mm > NUM_MESI || mm == 0) { + cout << "Mese inserito non valido!" << endl; + return; + } + + sprintf(data, "%02i %s %04i", gg, mesi[mm - 1], aaaa); + + cout << "Data formattata: " << data << endl; +} \ No newline at end of file diff --git a/sfusi/getDigit.cpp b/sfusi/getDigit.cpp new file mode 100644 index 0000000..60a4acd --- /dev/null +++ b/sfusi/getDigit.cpp @@ -0,0 +1,58 @@ +/* +Nome: Mario +Cognome: Montanari + +getDigit +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *getDigit(char *const str); + +int main(void) { + char str[SIZE] = "H3ll0 W0rld! 2025"; + + cout << "Before: " << str << endl; + + getDigit(str); + + cout << "After: " << str << endl; + + return 0; +} + +// Con puntatori +char *getDigit(char *const str) { + char *w = str; + + for (char *r = str; *r != '\0'; r++) { + if (isdigit(*r)) { + *w++ = *r; + } + } + + *w = '\0'; + + return str; +} + +/* Senza puntatori +char *getDigit(char *const str) { + int w = 0; + + for (int r = 0; str[r] != '\0'; r++) { + if (isdigit(str[r])) { + str[w++] = str[r]; + } + } + + str[w] = '\0'; + + return str; +} +*/ \ No newline at end of file diff --git a/sfusi/isAnagram.cpp b/sfusi/isAnagram.cpp new file mode 100644 index 0000000..4d235a7 --- /dev/null +++ b/sfusi/isAnagram.cpp @@ -0,0 +1,70 @@ +/* +Nome: Mario +Cognome: Montanari + +isAnagram +*/ + +#include +#include +#include + +#define SIZE 100+1 +#define NUM_LETT 26 + +using namespace std; + +bool isAnagram(char *src1, char *src2); + +int main(void) { + char src1[SIZE]; + char src2[SIZE]; + + cout << "Prima parola: "; + cin.getline(src1, SIZE); + + cout << "Seconda parola: "; + cin.getline(src2, SIZE); + + if (isAnagram(src1, src2)) { + cout << "Le due parole sono anagrammi." << endl; + } else { + cout << "Le due parole non sono anagrammi." << endl; + } + + return 0; +} + +bool isAnagram(char *src1, char *src2) { + for (int i = 0; i < strlen(src1); i++) { + src1[i] = tolower(src1[i]); + } + + for (int i = 0; i < strlen(src2); i++) { + src2[i] = tolower(src2[i]); + } + + if (strlen(src1) != strlen(src2)) { + return false; + } + + for (int i = 0; i < strlen(src1); i++) { + if (strchr(src2, src1[i]) == NULL) { + return false; + } + } + + int count[26] = {0}; + for (int i = 0; i < strlen(src1); i++) { + count[tolower(src1[i]) - 'a']++; + count[tolower(src2[i]) - 'a']--; + } + + for (int i = 0; i < NUM_LETT; i++) { + if (count[i] != 0) { + return false; + } + } + + return true; +} \ No newline at end of file diff --git a/sfusi/isMonovocalic.cpp b/sfusi/isMonovocalic.cpp new file mode 100644 index 0000000..eaf6ad5 --- /dev/null +++ b/sfusi/isMonovocalic.cpp @@ -0,0 +1,82 @@ +/* +Nome: Mario +Cognome: Montanari + +isMonovocalic +*/ + +#include +#include + +#define SIZE 30+1 + +using namespace std; + +/* +int isConsonant(char c); +*/ + +int isVowel(char c); +void isMonovocalic(const char *str); + +int main(void) { + char str[SIZE]; + + cout << "Inserisci una parola: "; + cin >> str; + + for (int i = 0; str[i] != '\0'; i++) { + str[i] = toupper(str[i]); + } + + isMonovocalic(str); + + return 0; +} + +/* +int isConsonant(char c) { + return ((c >= 'A' && c <= 'Z') || (c > 'a' && c <= 'z')) + && !isVowel(c); +} +*/ + +int isVowel(char c) { + return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' + || c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; +} + +void isMonovocalic(const char *str) { + bool isA = false; + bool isE = false; + bool isI = false; + bool isO = false; + bool isU = false; + + for (int i = 0; str[i] != '\0'; i++) { + if (str[i] == 'A') { + isA = true; + } else if (str[i] == 'E') { + isE = true; + } else if (str[i] == 'I') { + isI = true; + } else if (str[i] == 'O') { + isO = true; + } else if (str[i] == 'U') { + isU = true; + } + } + + int vowelCount = 0; + if (isA) vowelCount++; + if (isE) vowelCount++; + if (isI) vowelCount++; + if (isO) vowelCount++; + if (isU) vowelCount++; + + if (vowelCount == 1) { + cout << "Parola monovocalica" << endl; + } else { + cout << "Parola non monovocalica" << endl; + } +} \ No newline at end of file diff --git a/sfusi/isPangram.cpp b/sfusi/isPangram.cpp new file mode 100644 index 0000000..1b77c28 --- /dev/null +++ b/sfusi/isPangram.cpp @@ -0,0 +1,54 @@ +/* +Nome: Mario +Cognome: Montanari + +isPangram +*/ + +#include +#include +#include + +#define SIZE 100+1 + +using namespace std; + +bool isPangram(char *src1); + +int main(void) { + char src1[SIZE]; + + cout << "Prima frase: "; + cin.getline(src1, SIZE); + + if (isPangram(src1)) { + cout << "La fare e' un Pangramma." << endl; + } else { + cout << "La frase non e' un pangramma." << endl; + } + + return 0; +} + +bool isPangram(char *src1) { + for (int i = 0; i < strlen(src1); i++) { + src1[i] = tolower(src1[i]); + } + + for (char ch = 'a'; ch <= 'z'; ch++) { + bool letterFound = false; + + for (int i = 0; i < strlen(src1); i++) { + if (src1[i] == ch) { + letterFound = true; + break; + } + } + + if (!letterFound) { + return false; + } + } + + return true; +} \ No newline at end of file diff --git a/sfusi/isSymmetricVowel.cpp b/sfusi/isSymmetricVowel.cpp new file mode 100644 index 0000000..dc3c58f --- /dev/null +++ b/sfusi/isSymmetricVowel.cpp @@ -0,0 +1,58 @@ +/* +Nome: Mario +Cognome: Montanari +*/ + +#include +#include +#include + +#define SIZE 100+1 + +using namespace std; + +bool isVowel(char chr); +bool isSymmetricVowel(const char *const str); + +int main(void) { + char str[SIZE]; + + cout << "Inserisci una parola: "; + cin.getline(str, SIZE); + + if (isSymmetricVowel(str)) { + cout << "Le vocali sono simmetriche!" << endl; + } else { + cout << "Le vocali non sono simmetriche!" << endl; + } + + return 0; +} + +bool isVowel(char chr) { + return (chr == 'A' || chr == 'a' + || chr == 'E' || chr == 'e' + || chr == 'I' || chr == 'i' + || chr == 'O' || chr == 'o' + || chr == 'U' || chr == 'u'); +} + +bool isSymmetricVowel(const char *const str) { + if (*str != '\0') { + for (const char *pl = str, *pr = str + strlen(str) - 1; pl < pr; pl++, pr--) { + while (*pl != '\0' && !isVowel(*pl)) { + pl++; + } + + while (pr >= str && !isVowel(*pr)) { + pr--; + } + + if (pl < pr && tolower(*pl) != tolower(*pr)) { + return false; + } + } + } + + return true; +} \ No newline at end of file diff --git a/sfusi/parolaCensurata.cpp b/sfusi/parolaCensurata.cpp new file mode 100644 index 0000000..e901297 --- /dev/null +++ b/sfusi/parolaCensurata.cpp @@ -0,0 +1,36 @@ +/* +Nome: Mario +Cognome: Montanari + +parolaCensurata +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *parolaCensurata(char *const str, const char *substr); + +int main(void) { + char str[] = "fuck all the world!"; + const char substr[] = "fuck"; + + cout << "Frase originale: " << str << endl; + + cout << "Frase censurata: " << parolaCensurata(str, substr); + + return 0; +} + +char *parolaCensurata(char *const str, const char *substr) { + int len = strlen(substr); + + for (char *pc = str; (pc = strstr(pc, substr)) != NULL; pc = pc + len) { + memset(pc + 1, '*', len - 2); + } + + return str; +} \ No newline at end of file diff --git a/sfusi/pigLatin.cpp b/sfusi/pigLatin.cpp new file mode 100644 index 0000000..bcc5e66 --- /dev/null +++ b/sfusi/pigLatin.cpp @@ -0,0 +1,62 @@ +/* +Nome: Mario +Cognome: Montanari + +pigLatin +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +bool isVowel(char chr); +void pigLatin(char *mssg); + +int main(void) { + char mssg[SIZE]; + + cout << "Inserisci una frase: "; + cin.getline(mssg, SIZE); + + pigLatin(mssg); + + return 0; +} + +bool isVowel(char chr) { + return ( + chr == 'A' || chr == 'a' || + chr == 'E' || chr == 'e' || + chr == 'I' || chr == 'i' || + chr == 'O' || chr == 'o' || + chr == 'U' || chr == 'u' + ); +} + +void pigLatin(char *mssg) { + char *token = strtok(mssg, " "); + + while (token != nullptr) { + int len = strlen(token); + + if (isVowel(token[0])) { + cout << token << "way "; + } else { + char first = token[0]; + for (int i = 0; i < len - 1; i++) { + token[i] = token[i + 1]; + } + token[len - 1] = first; + token[len] = '\0'; + + cout << token << "ay "; + } + + token = strtok(nullptr, " "); + } + + cout << endl; +} \ No newline at end of file diff --git a/sfusi/removeDigit.cpp b/sfusi/removeDigit.cpp new file mode 100644 index 0000000..cc7e754 --- /dev/null +++ b/sfusi/removeDigit.cpp @@ -0,0 +1,58 @@ +/* +Nome: Mario +Cognome: Montanari + +removeDigit (come delDigit) +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *removeDigit(char *const str); + +int main(void) { + char str[SIZE] = "H3ll0 W0rld! 2025"; + + cout << "Before: " << str << endl; + + removeDigit(str); + + cout << "After: " << str << endl; + + return 0; +} + +// Con puntatori +char *removeDigit(char *const str) { + char *w = str; + + for (char *r = str; *r != '\0'; r++) { + if (!isdigit(*r)) { + *w++ = *r; + } + } + + *w = '\0'; + + return str; +} + +/* Senza puntatori +char *removeDigit(char *const str) { + int w = 0; + + for (int r = 0; str[r] != '\0'; r++) { + if (!isdigit(str[r])) { + str[w++] = str[r]; + } + } + + str[w] = '\0'; + + return str; +} +*/ \ No newline at end of file diff --git a/sfusi/strcasecmp().cpp b/sfusi/strcasecmp().cpp new file mode 100644 index 0000000..91fdd00 --- /dev/null +++ b/sfusi/strcasecmp().cpp @@ -0,0 +1,56 @@ +/* +Nome: Mario +Cognome: Montanari + +strcasecmp() +*/ + +#include +#include +#include + +#define SIZE 100+1 + +using namespace std; + +int strcasecmp(const char *src1, const char *src2); + +int main(void) { + char src1[SIZE]; + char src2[SIZE]; + int result; + + cout << "Inserisci una prima frase: "; + cin.getline(src1, SIZE); + + cout << "Inserisci una seconda frase: "; + cin.getline(src2, SIZE); + + result = strcasecmp(src1, src2); + + if (result == 0) { + cout << endl << "src1 = src2" << endl; + } else if (result < 0) { + cout << endl << "src1 < src2" << endl; + } else { + cout << endl << "src1 > src2" << endl; + } + + return 0; +} + +int strcasecmp(const char *src1, const char *src2) { + while (*src1 && src2) { + char chr1 = tolower(*src1); + char chr2 = tolower(*src2); + + if (chr1 != chr2) { + return chr1 - chr2; + } + + src1++; + src2++; + } + + return tolower(*src1) - tolower(*src2); +} \ No newline at end of file diff --git a/sfusi/strcat().cpp b/sfusi/strcat().cpp new file mode 100644 index 0000000..e495125 --- /dev/null +++ b/sfusi/strcat().cpp @@ -0,0 +1,43 @@ +/* +Nome: Mario +Cognome: Montanari + +strcat() +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *strcat(char *src1, const char *src2); + +int main(void) { + char src1[SIZE]; + char src2[] = "@studenti.itisravenna.it"; + char *ptr; + + cout << "Inserisci il nome utente della tua email scolastica: "; + cin >> src1; + + ptr = strcat(src1, src2); + + cout << "Email scolastica: " << ptr << endl; + + return 0; +} + +char *strcat(char *src1, const char *src2) { + int i; + int len = strlen(src1); + + for (i = 0; src2[i] != '\0'; i++) { + src1[len + i] = src2[i]; + } + + src1[len + i] = '\0'; + + return src1; +} \ No newline at end of file diff --git a/sfusi/strchr().cpp b/sfusi/strchr().cpp new file mode 100644 index 0000000..5bd698f --- /dev/null +++ b/sfusi/strchr().cpp @@ -0,0 +1,44 @@ +/* +Nome: Mario +Cognome: Montanari + +strchr() +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *strchr(const char *src, int c); + +int main(void) { + char src[SIZE]; + char *ptr; + int chr = 'a'; + + cout << "Inserisci una parola o una frase: "; + cin.getline(src, SIZE); + + ptr = strchr(src, chr); + + if (ptr != nullptr) { + cout << ptr << endl; + } else { + cout << "Character not found!" << endl; + } + + return 0; +} + +char *strchr(const char *src, int c) { + do { + if (*src == c) { + return (char*)src; + } + } while (*src++); + + return NULL; +} \ No newline at end of file diff --git a/sfusi/strcmp().cpp b/sfusi/strcmp().cpp new file mode 100644 index 0000000..26d1d55 --- /dev/null +++ b/sfusi/strcmp().cpp @@ -0,0 +1,49 @@ +/* +Nome: Mario +Cognome: Montanari + +strcmp() +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +int strcmp(const char *src1, const char *src2); + +int main(void) { + char src1[SIZE]; + char src2[SIZE]; + int result; + + cout << "Inserisci una prima frase: "; + cin.getline(src1, SIZE); + + cout << "Inserisci una seconda frase: "; + cin.getline(src2, SIZE); + + result = strcmp(src1, src2); + + if (result == 0) { + cout << endl << "src1 = src2" << endl; + } else if (result < 0) { + cout << endl << "src1 < src2" << endl; + } else { + cout << endl << "src1 > src2" << endl; + } + + return 0; +} + +int strcmp(const char *src1, const char *src2) { + for (; *src1 == *src2; src1++, src2++) { + if (*src1 == '\0') { + return 0; + } + } + + return *src1 - *src2; +} \ No newline at end of file diff --git a/sfusi/strcpy().cpp b/sfusi/strcpy().cpp new file mode 100644 index 0000000..7ec1eaa --- /dev/null +++ b/sfusi/strcpy().cpp @@ -0,0 +1,42 @@ +/* +Nome: Mario +Cognome: Montanari + +strcpy() +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *strcpy(char *dst, const char *src); + +int main(void) { + char src[SIZE] = "mario.montanari@studenti.itisravenna.it"; + char dst[SIZE]; + char *ptr; + + ptr = strcpy(dst, src); + + cout << src << endl; + cout << ptr << endl; + + return 0; +} + +char *strcpy(char *dst, const char *src) { + const char *ptr = dst; + + while (*src) { + *dst = *src; + dst++; + src++; + } + + *dst = '\0'; + + return (char*)ptr; +} \ No newline at end of file diff --git a/sfusi/strncat().cpp b/sfusi/strncat().cpp new file mode 100644 index 0000000..2955f7d --- /dev/null +++ b/sfusi/strncat().cpp @@ -0,0 +1,47 @@ +/* +Nome: Mario +Cognome: Montanari + +strncat() +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *strncat(char *dst, const char *src, int n); + +int main(void) { + char src[SIZE] = "mario.montanari@studenti.itisravenna.it"; + char dst[SIZE]; + char *ptr; + + ptr = strncat(dst, src, 15); + + cout << src << endl; + cout << ptr << endl; + + return 0; +} + +char *strncat(char *dst, const char *src, int n) { + const char *ptr = dst; + + while (*dst) { + *dst++; + } + + while (n > 0 && *src) { + *dst = *src; + dst++; + src++; + n--; + } + + *dst = '\0'; + + return (char*)ptr; +} \ No newline at end of file diff --git a/sfusi/strncmp().cpp b/sfusi/strncmp().cpp new file mode 100644 index 0000000..4bc8c68 --- /dev/null +++ b/sfusi/strncmp().cpp @@ -0,0 +1,55 @@ +/* +Nome: Mario +Cognome: Montanari + +strncmp() +*/ + +#include + +#define SIZE 40 + +using namespace std; + +int strncmp(const char *src1, const char *src2, int count); + +int main(void) { + char src1[SIZE]; + char src2[SIZE]; + int result; + + cout << "Inserisci una prima frase: "; + cin.getline(src1, SIZE); + + cout << "Inserisci una seconda frase: "; + cin.getline(src2, SIZE); + + result = strncmp(src1, src2, 1); + + if (result == 0) { + cout << "src1 = src2" << endl; + } else if (result < 0) { + cout << "src1 < src2" << endl; + } else { + cout << "src1 > src2" << endl; + } + + return 0; +} + +int strncmp(const char *src1, const char *src2, int count) { + int i = 0; + + while (i < count && src1[i] != '\0' && src2[i] != '\0') { + if (src1[i] != src2[i]) { + return src1[i] - src2[i]; + } + i++; + } + + if (i < count) { + return src1[i] - src2[i]; + } + + return 0; +} \ No newline at end of file diff --git a/sfusi/strncpy().cpp b/sfusi/strncpy().cpp new file mode 100644 index 0000000..ee7eece --- /dev/null +++ b/sfusi/strncpy().cpp @@ -0,0 +1,49 @@ +/* +Nome: Mario +Cognome: Montanari + +strncpy() +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *strncpy(char *dst, const char *src, int n); + +int main(void) { + char src[SIZE] = "mario.montanari@studenti.itisravenna.it"; + char dst[SIZE]; + char *ptr; + + ptr = strncpy(dst, src, 15); + + cout << src << endl; + cout << ptr << endl; + + return 0; +} + +char *strncpy(char *dst, const char *src, int n) { + const char *ptr = dst; + + while (n > 0 && *src) { + *dst = *src; + dst++; + src++; + n--; + } + + while (n > 0) { + *dst = '\0'; + dst++; + n--; + } + + *dst = '\0'; + + return (char*)ptr; +} \ No newline at end of file diff --git a/sfusi/strpad.cpp b/sfusi/strpad.cpp new file mode 100644 index 0000000..958072d --- /dev/null +++ b/sfusi/strpad.cpp @@ -0,0 +1,35 @@ +/* +Nome: Mario +Cognome: Montanari + +strpad usando C++. +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *strpad(char *const str, unsigned start, unsigned end, char pad); + +int main(void) { + char str[SIZE] = "Buonasera"; + + cout << str << endl; + cout << strpad(str, 4, 3, '*') << endl; + + return 0; +} + +char *strpad(char *const str, unsigned start, unsigned end, char pad) { + memmove(str + start, str, strlen(str) + 1); + memset(str, pad, start); + + str[strlen(str) + end] = '\0'; + + memset(str + strlen(str), pad, end); + + return str; +} \ No newline at end of file diff --git a/sfusi/strpbrk().cpp b/sfusi/strpbrk().cpp new file mode 100644 index 0000000..ca9b8e7 --- /dev/null +++ b/sfusi/strpbrk().cpp @@ -0,0 +1,46 @@ +/* +Nome: Mario +Cognome: Montanari + +strpbrk() +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *strpbrk(const char *src1, const char *src2); + +int main(void) { + char src1[SIZE] = "mario.montanari@studenti.itisravenna.it"; + char src2[SIZE] = "aeiou"; + char *ptr; + + ptr = strpbrk(src1, src2); + + cout << src1 << endl; + + if (ptr != nullptr) { + cout << ptr << endl; + } else { + cout << "Character not found!" << endl; + } + + return 0; +} + +char *strpbrk(const char *src1, const char *src2) { + while (*src1) { + for (const char *ptr = src2; *ptr != '\0'; ptr++) { + if (*src1 == *ptr) { + return (char*)src1; + } + } + src1++; + } + + return nullptr; +} \ No newline at end of file diff --git a/sfusi/strrchr().cpp b/sfusi/strrchr().cpp new file mode 100644 index 0000000..f9596fd --- /dev/null +++ b/sfusi/strrchr().cpp @@ -0,0 +1,41 @@ +/* +Nome: Mario +Cognome: Montanari + +strrchr() +*/ + +#include +#include + +#define SIZE 40 + +using namespace std; + +char *strrchr(const char *src, int c); + +int main(void) { + char src[SIZE] = "mario.montanari@studenti.itisravenna.it"; + char *ptr; + int chr = 'a'; + + ptr = strrchr(src, chr); + + cout << src << endl; + cout << ptr << endl; + + return 0; +} + +char *strrchr(const char *src, int c) { + const char *ptr = nullptr; + + while (*src) { + if (*src == c) { + ptr = src; + } + src++; + } + + return (char*)ptr; +} \ No newline at end of file diff --git a/sfusi/strstr().cpp b/sfusi/strstr().cpp new file mode 100644 index 0000000..f317659 --- /dev/null +++ b/sfusi/strstr().cpp @@ -0,0 +1,56 @@ +/* +Nome: Mario +Cognome: Montanari + +strstr() +*/ + +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *strstr(const char *src1, const char *src2); + +int main(void) { + char src1[SIZE] = "Ciao a tutti, come state?"; + char src2[SIZE] = "tutti"; + char *result; + + cout << "Stringa iniziale: " << src1 << endl; + cout << "Sottostringa da trovare: " << src2 << endl; + + result = strstr(src1, src2); + + if (result != nullptr) { + cout << endl << "Sottostringa trovata: " << result << endl; + } else { + cout << endl << "Sottostringa non trovata." << endl; + } + + return 0; +} + +char *strstr(const char *src1, const char *src2) { + if (!*src2) return (char*)src1; + + while (*src1) { + const char *ptr1 = src1; + const char *ptr2 = src2; + + while (*ptr1 && *ptr2 && *ptr1 == *ptr2) { + ptr1++; + ptr2++; + } + + if (!*ptr2) { + return (char*)src1; + } + + src1++; + } + + return nullptr; +} \ No newline at end of file diff --git a/sfusi/struct_libri.cpp b/sfusi/struct_libri.cpp new file mode 100644 index 0000000..f61c92a --- /dev/null +++ b/sfusi/struct_libri.cpp @@ -0,0 +1,142 @@ +/* +Nome: Mario +Cognome: Montanari + +Si crei la struttura libro, in cui ogni libro ha +un codice numerico (numero intero) che lo caratterizza, +un titolo, un autore, un numero di pagine e un costo. + - si memorizzino i dati di tre libri; + - si calcoli il costo medio per pagina dei libri; + - si stampino i dati dei tre libri in ordine crescente di costo per pagina; + - si stampino i dati dei tre libri in ordine crescente di titolo. + +*/ + +#include +#include +#include + +#define SIZE 100+1 + +#define N_LIBRI 3 + +#define MIN_SILLABE 2 +#define MAX_SILLABE 5 + +#define MIN_COD 10000 +#define MAX_COD 99999 + +#define MIN_TITOLO 65 +#define MAX_TITOLO 90 + +#define MIN_PAG 100 +#define MAX_PAG 900 + +#define MIN_COSTO 1000 +#define MAX_COSTO 9000 + +using namespace std; + +typedef struct { + int nCod; + char titolo[SIZE]; + char autore[SIZE]; + int nPag; + float costo; + int costoPerPagina; +} libro; + +int cmpCostoPerPagina(const void *x, const void *y); +int cmpTitolo(const void *x, const void *y); +void generaCaratteristiche(libro caratteristiche[]); +void stampaCaratteristiche(libro caratteristiche[]); + +int main(void) { + srand(time(NULL)); + libro caratteristiche[N_LIBRI]; + int nSillabe = 0; + + generaCaratteristiche(caratteristiche); + + cout << "--- Ordinazione crescente in base al costo medio per pagina --------------------------------------------" << endl << endl; + qsort(caratteristiche, N_LIBRI, sizeof(libro), cmpCostoPerPagina); + stampaCaratteristiche(caratteristiche); + + cout << endl << endl << endl << "--- Ordinazione crescente in base al titolo ------------------------------------" << endl << endl; + qsort(caratteristiche, N_LIBRI, sizeof(libro), cmpTitolo); + stampaCaratteristiche(caratteristiche); + + return 0; +} + +int cmpCostoPerPagina(const void *x, const void *y) { + const libro *libroX = (const libro *)x; + const libro *libroY = (const libro *)y; + + return libroY->costoPerPagina - libroX->costoPerPagina; +} + +int cmpTitolo(const void *x, const void *y) { + const libro *libroX = (const libro *)x; + const libro *libroY = (const libro *)y; + + return strcmp(libroX->titolo, libroY->titolo); +} + +void generaCaratteristiche(libro caratteristiche[]) { + const char vocali[] = "AEIOU"; + const char consonanti[] = "BCDFGHJKLMNPQRSTVWXYZ"; + + for (int i = 0; i < N_LIBRI; i++) { + + caratteristiche[i].nCod = rand() % (MAX_COD - MIN_COD + 1) + MIN_COD; + // 'setfill('.')' riempie gli spazi vuoti con dei punti + + int nSillabe = rand() % (MAX_SILLABE - MIN_SILLABE + 1) + MIN_SILLABE; + + int r = 0; + for (int j = 0; j < nSillabe; j++) { + caratteristiche[i].titolo[r++] = consonanti[rand() % 21]; + caratteristiche[i].titolo[r++] = vocali[rand() % 5]; + } + caratteristiche[i].titolo[r] = '\0'; + + nSillabe = rand() % (MAX_SILLABE - MIN_SILLABE + 1) + MIN_SILLABE; + + int w = 0; + for (int j = 0; j < nSillabe; j++) { + caratteristiche[i].autore[w++] = consonanti[rand() % 16]; + caratteristiche[i].autore[w++] = vocali[rand() % 5]; + } + + caratteristiche[i].autore[w++] = ' '; + + nSillabe = rand() % (MAX_SILLABE - MIN_SILLABE + 1) + MIN_SILLABE; + + for (int j = 0; j < nSillabe; j++) { + caratteristiche[i].autore[w++] = consonanti[rand() % 16]; + caratteristiche[i].autore[w++] = vocali[rand() % 5]; + } + caratteristiche[i].autore[w] = '\0'; + + caratteristiche[i].nPag = rand() % (MAX_PAG - MIN_PAG + 1) + MIN_PAG; + + caratteristiche[i].costo = rand() % (MAX_COSTO - MIN_COSTO + 1) + MIN_COSTO; + + caratteristiche[i].costoPerPagina = caratteristiche[i].costo / caratteristiche[i].nPag; + // 'fixed' imposta il numero in virgola fissa + // 'setprecision(2)' limita a 2 cifre decimali il numero + } +} + +void stampaCaratteristiche(libro caratteristiche[]) { + for (int i = 0; i < N_LIBRI; i++) { + cout << "Libro " << "'" << caratteristiche[i].titolo << "':" << endl; + cout << " Codice numerico: " << setfill('.') << setw(18) << " " << caratteristiche[i].nCod << endl; + cout << " Titolo: " << setw(27) << " " << caratteristiche[i].titolo << endl; + cout << " Autore: " << setw(27) << " " << caratteristiche[i].autore << endl; + cout << " Numero pagine: " << setw(20) << " " << caratteristiche[i].nPag << " pagine" << endl; + cout << " Costo: " << setw(28) << " " << caratteristiche[i].costo / 100 << " euro" << endl; + cout << " Costo medio per pagina: " << setw(11) << fixed << setprecision(2) << " " << caratteristiche[i].costoPerPagina << " centesimi" << endl << endl; + } +} \ No newline at end of file diff --git a/sfusi/tripleDigit.cpp b/sfusi/tripleDigit.cpp new file mode 100644 index 0000000..4eb2ea9 --- /dev/null +++ b/sfusi/tripleDigit.cpp @@ -0,0 +1,73 @@ +/* +Nome: Mario +Cognome: Montanari + +tripleDigit +*/ + +#include +#include +#include + +#define SIZE 100+1 + +using namespace std; + +char *tripleDigit(char *const str); + +int main(void) { + char str[SIZE] = "H3ll0 W0rld! 2025"; + + cout << "Before: " << str << endl; + + tripleDigit(str); + + cout << "After: " << str << endl; + + return 0; +} + +// Con puntatori +char *tripleDigit(char *const str) { + int len = strlen(str); + + for (char *j = str; j - str < len && len + 2 < SIZE - 1; j++) { + if (isdigit(*j)) { + if (len + 2 >= SIZE) break; + + memmove(j + 3, j + 1, len - (j - str) + 1); + + *(j + 1) = *j; + *(j + 2) = *j; + + len = len + 2; + j = j + 2; + } + } + + return str; +} + +/* Senza puntatori +char *tripleDigit(char *const str) { + int len = strlen(str); + + for (int j = 0; j < len && len + 2 < SIZE - 1; j++) { + if (isdigit(str[j])) { + if (len + 2 >= SIZE) break; + + for (int k = len; k >= j; k--) { + str[k + 2] = str[k]; + } + + str[j + 1] = str[j]; + str[j + 2] = str[j]; + + len = len + 2; + j = j + 2; + } + } + + return str; +} +*/ \ No newline at end of file