Guest User

Untitled

a guest
Oct 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. int main()
  4. {
  5. //to count number of times a button is pressed
  6. int index=0;
  7. //to store the input sequence
  8. char input[50];
  9. //iterator through input
  10. int iterator = 0;
  11.  
  12. printf("\n>Hello :) Welcome to the virtual mobile keypad\n");
  13. //read the code
  14. printf("\n>Enter code to decode : ");
  15. gets(input);
  16. printf("\n>You entered : ");
  17. //iterate through all the input characters
  18. while(input[iterator]!='\0')
  19. {
  20. //if consecutive characters(numbers) are same, then increment the index
  21. for(;input[iterator+1]!='\0';iterator++)
  22. {
  23. if(input[iterator]==input[iterator+1])
  24. index++;
  25. else
  26. break;
  27. }
  28. //if index is greater than number of characters a number can represent(i.e. digit 2 represent 3 characters a,b,c),
  29. //take the modulus to have circular pattern i.e a,b,c,a,b,c... )
  30. if(input[iterator]=='9' || input[iterator]=='7')
  31. {
  32. if(index>=4)
  33. index%=4;
  34. }
  35. else
  36. {
  37. index%=3;
  38. }
  39. switch(input[iterator])
  40. {
  41. case '2': printf("%c",'a'+index);//if index = 0, print 'a' //if index = 1, print b
  42. break;
  43. case '3': printf("%c",'d'+index);
  44. break;
  45. case '4': printf("%c",'g'+index);
  46. break;
  47. case '5': printf("%c",'j'+index);
  48. break;
  49. case '6': printf("%c",'m'+index);
  50. break;
  51. case '7': printf("%c",'p'+index);
  52. break;
  53. case '8': printf("%c",'t'+index);
  54. break;
  55. case '9': printf("%c",'w'+index);
  56. break;
  57.  
  58. case ' ': break; //' ' indicates a pause in input
  59.  
  60. case '0': printf(" "); //'0' indicates a space
  61. break;
  62. default: printf("\n\nError : Enter only numbers and space\n\n");
  63. //if other than 0-9 and ' ' is entered
  64. exit(0);
  65. }
  66. index=0;
  67. iterator++;
  68. }
  69. printf("\n\n");
  70. return 0;
  71. }
Add Comment
Please, Sign In to add comment