Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to programming. These are my codes:
C#
public string ThanglishToTamilList(char[] characters, int length) {
        var dict1 = new Dictionary<string, string>();

        dict1.Add("a", "\u0B85"); //
        dict1.Add("aa", "\u0B86"); //
        dict1.Add("A", "\u0B86"); //
        dict1.Add("i", "\u0B87"); //
        dict1.Add("ee", "\u0B88"); //
        dict1.Add("I", "\u0B88"); //
        dict1.Add("u", "\u0B89"); //
        ...



        List<String> list = new List<String>();
        string[] array;
        var valueOfDictOne = "";

        for (int i = 0; i < length; i++)
        {
            try
            {
                valueOfDictOne = dict1[characters[i].ToString()];
                list.Add(valueOfDictOne);

            }
            catch
            {
                list.Add(characters[i].ToString());
            }
        }

        array = list.ToArray();
        string result = string.Join("", array);
        return result;
    }

function Parameter details:

char[] characters : Array of characters (textbox.text.ToCharArray())

int length : length of the array. (no of characters we typed in the text box)

My expected output should be:

If the user types a -> Output should be அ.

Likewise:

a -> அ

aa -> ஆ

A -> ஆ ...

note that aa & A represent same ஆ

My Problem: This code only replace one charecter (a -> அ), This works fine.

But if we type aa the output is அஅ

aa -> அஅ

But I need the correct output as

aa -> ஆ

I have added some lines of codes for this. but this did not work:
C#
...
       for (int i = 0; i < length; i++)
       {
           try
           {

               if (String.Equals(characters[i], "a") && !(String.Equals(characters[i], "aa")))
               {

                   //MessageBox.Show("a");

                   valueOfDictOne = dict1[characters[i].ToString()];
                   list.Add(valueOfDictOne);
               }
               else if (String.Equals(characters[i], "aa"))
               {
                   //MessageBox.Show("aa");

                   valueOfDictOne = dict1[characters[i].ToString()];
                   list.Add(valueOfDictOne);
               }

           }
           catch
           {
               list.Add(characters[i].ToString());
           }
       }

SQL
Please help me to correct this code or please provide any easy alternative ways to transliterate.

Thank you.
Posted
Updated 13-May-13 2:31am
v2

The problem is that your code is very simplistic: you find the first match and go with it. which really won't work. Instead of processing each character individually though the string, you need to process the list of valid replacements instead, and that means that you need to introduce an order to your list as well. For example, you need to process all the examples of "aa" in your string before you start looking at any examples of "a", or the later will always get precedence.

For example, what you need to do is along the lines of:
C#
string s = characters.ToSting();
s = s.Replace("aa", "\u0B86"");
s = s.Replace("a", "\u0B85");


I think you need to sit down and rethink, because a dictionary is not a good choice for this - the order of values is determined by the order in which you insert them, and you need control over the ordering of your commands.


BTW: It is a very bad practice to rely on try catch for normal processing - it is very expensive in processing time compared to checking for problems before you try the code.
 
Share this answer
 
Comments
Suthan-S 14-May-13 0:47am    
Thanks it works.:)
OriginalGriff 14-May-13 3:56am    
You're welcome!
Please consider the alternative I suggested in my past answer: Problem in typing in Hindi and english[^].

What do you think? The problem is: Tamil has over 60 characters. I explained how it could be worked around.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900