PLE:Unidad4Ejercicio24

/* Programa:        Unidad4Ejercicio24.cpp Fecha:           30/01/2007 Descripción:     Lee una cadena y cuenta frecuancia de caracteres. using namespace std;
 * 1) include
 * 2) include
 * 3) include
 * 4) include
 * 1) include

struct elemento {       char car; unsigned int cont; };

const int NUM = 80;

void inserta(elemento f[], unsigned int &n, char c); void muestra(elemento f[], unsigned int n); int busca(elemento f[], unsigned int n, char c);

void main {       char cadena[80]; elemento frec[NUM]; unsigned int numElem = 0, i, p;

cout << "Introduzca una cadena "; cin.getline(cadena,80);

for(i = 0; i < strlen(cadena); i++) {               p = busca(frec, numElem, cadena[i]); if(p == -1) inserta(frec, numElem, cadena[i]); }

muestra(frec, numElem);

cin.ignore; cin.get; }

void inserta(elemento f[], unsigned int &n, char c) { f[n].car = c;       f[n].cont = 1; n++; }

void muestra(elemento f[], unsigned int n) { cout << "Car.\tNum\n"; for(unsigned int i = 0; i < n; i++) cout << f[i].car << '\t' << f[i].cont << endl; }

int busca(elemento f[], unsigned int n, char c) { int pos = -1, i = 0; while(i < n && pos == -1) {               if(f[i].car == c)                        pos = i;                i++; }       return pos; }