Created
December 20, 2015 21:12
-
-
Save jordansinger/90983a817dd3aeea4846 to your computer and use it in GitHub Desktop.
keypress.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "string.h" | |
#include "stdlib.h" | |
#include "stdio.h" | |
int NUM_KEYS = 9; // 2 - 9 and 0 | |
struct Number { | |
int number; // number on keypad | |
char *letters; // letters associated with number | |
int hits; // number of times pressed | |
}; | |
struct Number keypad[] = { | |
{ 0, " ", -1 }, | |
{ 2, "abc", -1 }, | |
{ 3, "def", -1 }, | |
{ 4, "ghi", -1 }, | |
{ 5, "jkl", -1 }, | |
{ 6, "mno", -1 }, | |
{ 7, "pqrs", -1 }, | |
{ 8, "tuv", -1 }, | |
{ 9, "wxyz", -1 } | |
}; | |
void pressKey(int key) { | |
keypad[key].hits += 1; | |
} | |
int main(int argc, char *args[]) { | |
args++; // skip first argument | |
int previousKey = -1; | |
while(*args) { | |
int key = atoi(*args); | |
if (key != 0) { | |
key -= 1; | |
} | |
if (previousKey == -1) { | |
previousKey = key; | |
} else if (previousKey != key) { | |
int letterToPrint = keypad[previousKey].hits % strlen(keypad[previousKey].letters); | |
printf("%c", keypad[previousKey].letters[letterToPrint]); | |
keypad[previousKey].hits = -1; | |
} else if (keypad[previousKey].hits == strlen(keypad[previousKey].letters) - 1) { | |
int letterToPrint = keypad[previousKey].hits; | |
printf("%c", keypad[previousKey].letters[letterToPrint]); | |
keypad[previousKey].hits = -1; | |
} | |
pressKey(key); | |
previousKey = key; | |
args++; | |
} | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment