#include <stdio.h>
#include <stdlib.h>
#define TAM 250
#define SUSTENIDO '#'

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	
	char msgCifra[TAM];
	int ndxCifra;
	char msgDecifrada[TAM];
	int ndxDecifra;
	char atual;
	int qtdeAtual;
	int tecla;
	int indice;
	
	char teclado[10][6]={{'\0', ' ', '0', '\0', '\0', '\0'},
						{'\0', '1', '\0', '\0', '\0', '\0'},
						{'\0', 'a', 'b', 'c', '2', '\0'},
						{'\0', 'd', 'e', 'f', '3','\0'},						
						{'\0', 'g', 'h', 'i', '4','\0'},
						{'\0', 'j', 'k', 'l', '5','\0'},						
						{'\0', 'm', 'n', 'o', '6','\0'},
						{'\0', 'p', 'q', 'r', 's', '7'},						
						{'\0', 't', 'u', 'v', '8','\0'},
						{'\0', 'w', 'x', 'y', 'z', '9'}						
						};
	
	int validos[10] = {2, 1, 4, 4, 4, 4, 4, 5, 4, 5};
	
	puts("Informe a mensagem cifrada");
	gets(msgCifra);
	
	ndxDecifra = 0;
	for(ndxCifra = 0; msgCifra[ndxCifra]; ndxCifra++){
		
		while(msgCifra[ndxCifra] == SUSTENIDO)
			ndxCifra++;   
		
		atual = msgCifra[ndxCifra];
		qtdeAtual = 1;

		while(msgCifra[ndxCifra] == msgCifra[ndxCifra+1]){
			qtdeAtual++;
			ndxCifra++;
		}
		tecla = atual - '0';
		indice = ((qtdeAtual - 1) % validos[tecla]) + 1; 
		msgDecifrada[ndxDecifra++] = teclado[tecla][indice];
	}

	msgDecifrada[ndxDecifra] = '\0';
	
	puts(msgDecifrada);
	
	return 0;
}
