66 lines
1.2 KiB
C++
66 lines
1.2 KiB
C++
/*
|
|
Nome: Mario
|
|
Cognome: Montanari
|
|
Classe: 3AIN
|
|
|
|
Inizializzare un array di elemento con una capacità massima
|
|
di 100 o di SIZE con le lettere dell'alfabeto e 0 nel campo numerico.
|
|
|
|
Contare le occorrenze delle lettere e memorizzarne il valore nell'array.
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <cstring>
|
|
#include <cctype>
|
|
|
|
#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;
|
|
} |