- 论坛徽章:
- 2
|
- /* digital pad */
- #include <ctype.h>
- #include <stdio.h>
- #include <string.h>
- #define ALEN(a) (sizeof(a)/sizeof((a)[0]))
- struct digital_map
- {
- char digit;
- const char *text;
- };
- static struct digital_map map_table[] =
- {
- {'2', "ABC"},
- {'3', "DEF"},
- {'4', "GHI"},
- {'5', "JKL"},
- {'6', "MNO"},
- {'7', "PRS"},
- {'8', "TUV"},
- {'9', "WXY"},
- };
- int char_to_digit(char c)
- {
- int i;
- for (i = 0; i < ALEN(map_table); ++i) {
- if (strchr(map_table[i].text, c) != NULL) {
- return map_table[i].digit;
- }
- }
- return c;
- }
- int main(void)
- {
- char ch;
- printf("Enter phone number: ");
- while ((ch = getchar()) != '\n') {
- putchar(char_to_digit(toupper(ch)));
- }
- putchar('\n');
- return 0;
- }
复制代码 |
|