Problem:
Extraordinary Girl (II)

She loves e-mail so much! She sends e-mails by her cellular phone to
her friends when she has breakfast, she talks with other friends, and even
when she works in the library! Her cellular phone has somewhat simple
layout (Figure 1). Pushing button 1 once displays a character (’), pushing

it twice in series displays a character (,), and so on, and pushing it 6 times
displays (’) again. Button 2 corresponds to charaters (abcABC), and, for
example, pushing it four times displays (A). Button 3-9 have is similar to
button 1. Button 0 is a special button: pushing it once make her possible
to input characters in the same button in series. For example, she has to
push “20202” to display “aaa” and “660666” to display “no”. In addition,
pushing button 0 n times in series (n > 1) displays n − 1 spaces. She never
pushes button 0 at the very beginning of her input. Here are some examples
of her input and output:

666660666 --> No
44444416003334446633111 --> I’m fine.
20202202000333003330333 --> aaba f ff

One day, the chief librarian of the library got very angry with her and
hacked her cellular phone when she went to the second floor of the library
to return books in shelves. Now her cellular phone can only display button
numbers she pushes. Your task is to write a program to convert the sequence
of button numbers into correct characters and help her continue her e-mails!

Input

Input consists of several lines. Each line contains the sequence of button
numbers without any spaces. You may assume one line contains no more
than 10000 numbers. Input terminates with EOF.

Output

For each line of input, output the corresponding sequence of characters in
one line.

Sample Input

666660666
44444416003334446633111
20202202000333003330333

Output for the Sample Input

No
I'm fine.
aaba f ff

Solution in JAVA (judge verdict: Accepted)

import java.util.Scanner;

public class Main {
    static final char NULL = '\0';

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            char[] input = sc.next().toCharArray();
            StringBuilder out = new StringBuilder();
            char buf = NULL;
            for (int i = 0; i < input.length; i++) {
                if (i != 0 && input[i] != input[i - 1]) {
                    if(buf!='0')
                    out.append(buf);
                    buf = NULL;
                }
                switch (input[i]) {
                case '1':
                    if (buf != NULL) {
                        switch (buf) {
                        case ',':
                            buf = '.';
                            break;
                        case '.':
                            buf = '!';
                            break;
                        case '!':
                            buf = '?';
                            break;
                        case '?':
                            buf = '\'';
                            break;
                        case '\'':
                            buf = ',';
                            break;
                        }
                    } else
                        buf = '\'';
                    break;
                case '2':
                    if (buf != NULL && buf != 'c' && buf != 'C') {
                        buf += 1;
                    } else if (buf == 'c') {
                        buf = 'A';
                    } else
                        buf = 'a';
                    break;
                case '3':
                    if (buf != NULL && buf != 'f' && buf != 'F') {
                        buf += 1;
                    } else if (buf == 'f') {
                        buf = 'D';
                    } else
                        buf = 'd';
                    break;
                case '4':
                    if (buf != NULL && buf != 'i' && buf != 'I') {
                        buf += 1;
                    } else if (buf == 'i') {
                        buf = 'G';
                    } else
                        buf = 'g';
                    break;
                case '5':
                    if (buf != NULL && buf != 'l' && buf != 'L') {
                        buf += 1;
                    } else if (buf == 'l') {
                        buf = 'J';
                    } else
                        buf = 'j';
                    break;
                case '6':
                    if (buf != NULL && buf != 'o' && buf != 'O') {
                        buf += 1;
                    } else if (buf == 'o') {
                        buf = 'M';
                    } else
                        buf = 'm';
                    break;
                case '7':
                    if (buf != NULL && buf != 's' && buf != 'S') {
                        buf += 1;
                    } else if (buf == 's') {
                        buf = 'P';
                    } else
                        buf = 'p';
                    break;
                case '8':
                    if (buf != NULL && buf != 'v' && buf != 'V') {
                        buf += 1;
                    } else if (buf == 'v') {
                        buf = 'T';
                    } else
                        buf = 't';
                    break;
                case '9':
                    if (buf != NULL && buf != 'z' && buf != 'Z') {
                        buf += 1;
                    } else if (buf == 'z') {
                        buf = 'W';
                    } else
                        buf = 'w';
                    break;
                case '0':
                    if (buf == '0') {
                        out.append(' ');
                    } else if (buf == NULL) {
                        buf = '0';
                    }
                    break;
                }
                if (i == input.length - 1)
                    out.append(buf);
            }
            System.out.println(out.toString());
        }
    }
}