c# - Using dictionary to convert string to char -


i've got code

 static string wordmap(string value)       {            var strings = value                .select(c =>                {                    string word;                    if (!wordmap.trygetvalue(c, out word))                        word = c.tostring();                    return word;                });            return string.join("", strings);        } 

and dictionary -

static dictionary<char, string> wordmap = new dictionary<char, string>() {    {'a', "alpha"}... } 

but instead of turning char words, want opposite. because i'm real beginner, couldn't figure out.

i saw there confusion meant so:

//i want  input = alpha beta gama dalta output = abgd //i input = alpha beta gama dalta output = alpha lama pyhton hexa alpha... 

it looks want reverse mapping string char. assumes you've done work

static dictionary<char, string> wordmap = new dictionary<char, string>() {     {'a', "alpha"}... } 

so make new mapping other direction:

static dictionary<string, char> charmap;  public static void convertlookup () {     charmap = wordmap.todictionary(e => e.value, e => e.key); } 

one thing assumes, however, strings unique.

edit: so, taking little further, understanding wanted go both ways. can put , this:

    static dictionary<char, string> wordmap = new dictionary<char, string>();     static dictionary<string, char> charmap = new dictionary<string, char>();      public static void initializelookups()     {         wordmap = new dictionary<char, string>         {             { 'a', "alice" },             { 'b', "bob" },             { 'c', "charlie" }         };          charmap = wordmap.todictionary(e => e.value, e => e.key);     }      public static string convertchartowords(string chars)     {         var strings = chars               .select(c =>               {                   string word;                   if (!wordmap.trygetvalue(c, out word))                       word = c.tostring();                   return word;               });         return string.join(" ", strings);     }     public static string convertwordstochars(string words)     {         var strings = words.split(' ')               .select(c =>               {                   char character;                   if (!charmap.trygetvalue(c, out character))                       character = '?';                   return character;               });         return string.join("", strings);     }      public static void main(string[] args)     {         initializelookups();         string words = convertchartowords("abc");         string chars = convertwordstochars(words);          console.writeline(words);         console.writeline(chars);     } 

input:

abc 

output:

alice bob charlie abc 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -