Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET4
I am new to programming. These are my codes:
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:
...
       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());
           }
       }
Please help me to correct this code or please provide any easy alternative ways to transliterate.
 
Thank you.
Posted 12-May-13 23:02pm
Edited 13-May-13 3:31am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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.
  Permalink  
Comments
Suthan-S at 14-May-13 0:47am
   
Thanks it works.:)
OriginalGriff at 14-May-13 3:56am
   
You're welcome!

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

  Print Answers RSS
0 OriginalGriff 459
1 Maciej Los 325
2 Sergey Alexandrovich Kryukov 234
3 DamithSL 233
4 BillWoodruff 200
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 13 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100