user4451265
user4451265

Reputation:

Reverse switch case statement

I want to reverse the code inside switch block rather doing it my self (it seems to make no sense to me) ex:

{            string name = Console.ReadLine();
             string s = null;
             for (int i = 0; i < name.Length; i++)
            {
                switch (name[i])
                {
                    case 'a':
                        s += '1';break;
                    case 'q':
                        s += '2'; break;
                 }
            }
}

How to make it reversed to act like this:

                    case '1':
                        s += 'a';break;
                    case '2':
                        s += 'q'; break;

My code contains more than 30 statements for each character.

Upvotes: 7

Views: 4509

Answers (3)

Patrick Hofman
Patrick Hofman

Reputation: 156978

Create a Dictionary<char, char> for the mapping (in fact you could create two for performance reasons and ease of use), or a simple list type with a custom object (like List<Tuple<char, char>>). You could make a method to make adding easier. Note that key and val must be unique!

private void Add(char key, char val, Dictionary<char, char> dictionary, Dictionary<char, char> reverseDictionary)
{
    dictionary.Add(key, val);
    reverseDictionary.Add(val, key);
}

Then use this:

Dictionary<char, char> dictionary = new Dictionary<char, char>();
Dictionary<char, char> reverseDictionary = new Dictionary<char, char>();

this.Add('a', '1', dictionary, reverseDictionary);

...

char outputChar;
if (dictionary.TryGetValue(inputChar, out outputChar))
{
    // use outputChar
}

And for the reverse:

char outputChar;
if (reverseDictionary.TryGetValue(inputChar, out outputChar))
{
    // use outputChar
}

Upvotes: 2

Icemanind
Icemanind

Reputation: 48686

You want something like this:

var dictionary = new Dictionary<char, char> {{'1', 'a'}, {'2', 'q'} /* ... */ };

string name = Console.ReadLine();
string s = name.Where(dictionary.ContainsKey)
               .Aggregate("", (current, t) => current + dictionary[t]);

Console.WriteLine(s);

Typing 12 will return aq. You can also reverse it:

string s = name.Where(dictionary.ContainsValue)
               .Aggregate("", (current, t) => current + dictionary.FirstOrDefault(z => z.Value == t).Key);

So now you can look up by value and get key. So typing aq will return 12.

Upvotes: 1

competent_tech
competent_tech

Reputation: 44931

One option is to create a dictionary to hold the mapping and then loop through the mapping building up the output string if the current character exists in the mapping dictionary.

The solution below shows using a dictionary of strings that ignore case and culture so that you don't have to have multiple entries for upper and lower case (this is obviously an optional design).

I am also showing the use of StringBuilder, which is far more efficient when appending strings.

var MappingDictionary = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);

MappingDictionary.Add("1", "a");
MappingDictionary.Add("2", "q");

var name = Console.ReadLine();
if (!string.IsNullOrEmpty(name))
{
    var s = new StringBuilder(500);

    foreach (var sourceChar in name)
    {
        string mappedTo;
        if (MappingDictionary.TryGetValue(sourceChar.ToString(), out mappedTo))
        {
            s.Append(mappedTo);
        }
    }
}

Upvotes: 0

Related Questions