Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- int main()
- {
- //to count number of times a button is pressed
- int index=0;
- //to store the input sequence
- char input[50];
- //iterator through input
- int iterator = 0;
- printf("\n>Hello :) Welcome to the virtual mobile keypad\n");
- //read the code
- printf("\n>Enter code to decode : ");
- gets(input);
- printf("\n>You entered : ");
- //iterate through all the input characters
- while(input[iterator]!='\0')
- {
- //if consecutive characters(numbers) are same, then increment the index
- for(;input[iterator+1]!='\0';iterator++)
- {
- if(input[iterator]==input[iterator+1])
- index++;
- else
- break;
- }
- //if index is greater than number of characters a number can represent(i.e. digit 2 represent 3 characters a,b,c),
- //take the modulus to have circular pattern i.e a,b,c,a,b,c... )
- if(input[iterator]=='9' || input[iterator]=='7')
- {
- if(index>=4)
- index%=4;
- }
- else
- {
- index%=3;
- }
- switch(input[iterator])
- {
- case '2': printf("%c",'a'+index);//if index = 0, print 'a' //if index = 1, print b
- break;
- case '3': printf("%c",'d'+index);
- break;
- case '4': printf("%c",'g'+index);
- break;
- case '5': printf("%c",'j'+index);
- break;
- case '6': printf("%c",'m'+index);
- break;
- case '7': printf("%c",'p'+index);
- break;
- case '8': printf("%c",'t'+index);
- break;
- case '9': printf("%c",'w'+index);
- break;
- case ' ': break; //' ' indicates a pause in input
- case '0': printf(" "); //'0' indicates a space
- break;
- default: printf("\n\nError : Enter only numbers and space\n\n");
- //if other than 0-9 and ' ' is entered
- exit(0);
- }
- index=0;
- iterator++;
- }
- printf("\n\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment