Re: [OT] Re: Increasing efficiency in C

From: Dan Pop (Dan.Pop_at_cern.ch)
Date: 03/05/04


Date: 5 Mar 2004 17:13:17 GMT

In <40488f13$0$167$a1866201@newsreader.visi.com> nospam@nowhere.com writes:

>Arthur J. O'Dwyer <ajo@nospam.andrew.cmu.edu> wrote:
>>> What algorithm you use for mapping 278487 to "Arthur" ??
>>
>> Hmm. I would have expected my explanation to be a little opaque
>> to a Russian native, perhaps, but I honestly expected the French
>> telephone to follow the American mold [see diagram]. Do your telephones
>> have any letters on the keys? Which ones?
>
><snip>
>
>He asked what ALGORITHM you used, not what an American telephone looks
>like.

The ALGORITHM is based on the layout of an American telephone keypad.

>It seemed readily obvious (to me, at least) that he wanted to
>know how std::string and std::map made your algorithm so much easier.

That's far from obvious. It's more likely that he didn't understand the
connection between 278487 and Arthur in the first place.

>I'd like to know as well.

There is little point in discussing std::string and std::map here, even
in an off topic thread, considering that we all know a better newsgroup
for this purpose.

If I were to write such a program, I'd assume CHAR_MAX to have a
reasonable value and build a lookup table for converting from letters
to digits. Then, a custom strlen-like function can be used to compare
the target string (composed of digits) with the candidate string
(composed of lower case and upper case letters).

    static char lut[CHAR_MAX + 1];

    void lutini(void)
    {
        lut['a'] = lut['A'] = '2';
        lut['b'] = lut['B'] = '2';
        ...
        lut['z'] = lut['Z'] = '9';
    }

    int match(char *phoneno, char *word)
    {
        int i, len = strlen(phoneno);

        if (strlen(word) != len) return 0;

        for (i = 0; i < len; i++)
            if (lut[word[i]] == phoneno[i]) continue;
            else return 0;

        return 1;
    }

The "word" string is supposed to be composed only of characters for
which isalpha() is true in the C locale. Other characters cannot be
mapped to the cellular phone keypad in a portable way.

Dan

-- 
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de


Relevant Pages