0

I have a string array containing strings of 3 letters each. Every 3 letters (every element) corresponds to a unique letter. I need to create a char array from the string array.

The only way i can think of doing this is looping through each element in the array of strings and using a long switch statement to determine its associated single character.

What other ways can this be accomplished?

rene
  • 41,474
  • 78
  • 114
  • 152
Singh
  • 301
  • 2
  • 6
  • 16
  • This is a chicken and egg problem -- if you had a data structure that you could use in place of a switch statement, then you wouldn't need to code a solution at all: you'd have the data structure you need already – Dexygen Oct 10 '13 at 18:44
  • Is there some function that determines what the single letter corresponding to each string is? It seems like the most direct approach, if there is, would be to implement that function. – Joshua Taylor Oct 10 '13 at 18:54
  • Are the 3-letter strings also unique, or can different strings be mapped to the same letter? – splungebob Oct 10 '13 at 18:56

4 Answers4

3

If it's a mapping/lookup then usually a map/dictionary solves your problem. An example such structure in C#:

string[] inList = new[]{"bee", "kay", "kay", "eff" }; 
Dictionary<string, char> mapping = new Dictionary<string, char> 
   { 
       {"bee", 'b'},
       {"eff", 'f'},
       {"kay", 'k'},
   };

If you have such a mapping, then just look up the letters from the mapping, or convert the whole list of strings to an array of chars.

char[] chars = inList.Select(s => mapping[s]).ToArray();

Almost all languages supports data structures of this type, although not all support functional constructs like the last snippet. In that case you need a loop to build the out array.

EDIT: Saw you added the java tag. You can accomplish the same in java, your dictionary will then be a HashMap in java. So just take an aspirin and look at How can I initialise a static Map?

Community
  • 1
  • 1
Anders Forsgren
  • 10,827
  • 4
  • 40
  • 77
3

I think what you need is a Hashmap mapping from string to char

Renaud
  • 4,569
  • 7
  • 41
  • 72
2

what language are you using?

you could set up a look-up table using an array like so:

lookuptable = { {'a','abc'},{'b','abd'} ....}

then you just look up the corresponding value in the array.

if your language supports dictionaries, it'll be even easier.

Loopo
  • 2,204
  • 2
  • 28
  • 45
0

You could use an Enum to define your elements and the corresponding character. Then have a method that decrypts the value.

Example:

public enum EncryptedValue {

    A ("A", "EDK"),
    B ("B", "CHI"),
    C ("C", "WAD"),
    ...;

    private String value;
    private String encryption;

    private static final List<EncryptedValue> VALUES = Collections.unmodifiableList(Arrays.asList(values()));

    private EncryptedValue(String value, String encryption) {
        this.value = value;
        this.encryption = encryption;
    }

    public String getValue() {
        return value;
    }

    public String getEncryption() {
        return encryption;
    }

    public String decrypt(Strign encryption) {
        for (EncryptedValue encryptedValue : VALUES) {
            if (encryptedValue.getEncryption().equalsIgnoreCase(encyrption))
                return encryptedValue.getValue();
        }

        return null;
    }

}
Joe
  • 661
  • 2
  • 8
  • 15