1

我有一堂课

class dictonary
{
  public string English{get;set;}
  public string Hindi{get;set;}
}

List<dictionary>在这里,我有一个在英语和印地语中具有含义的类词典列表。我想要作为

public string EngToHindi(string English)
{
   private string hindi="";
   //nepali = english =>Logic goes here
   return hindi;
}

如果我像这样传递字符串,"My Name Is Manoj." 它必须返回"Mera Naam hai Manoj"

List<dictionary>我有数据数据

   English   Hindi
---------------------
    my        mera
    is        hai
    Name     naam
4

2 回答 2

2

使用字典类。它完美无缺。这是您可以使用的完整程序。这很容易,它将定义整条线!!快乐编码!;)

class Program
{
    private static Dictionary<string, string> myDictionary;
    static void Main(string[] args)
    {
        // initialize your dictionary
        myDictionary = new Dictionary<string, string>(); 

        // fill the dictionary
        // you should fill if from file or database or something!
        myDictionary.Add("my", "mera");
        myDictionary.Add("is", "hai");
        myDictionary.Add("name", "naam");

        // the line you want to define in english:
        string line = "my name is Shamim";

        // output defined line in hindi:
        string output = EngLineToHindi(line);
        Console.WriteLine(output);
        Console.ReadKey();
    }
    static string EngLineToHindi(string line)
    {
        // array of words:
        string[] words = line.Split(' ');
        string ToReturn = "";
        foreach (string word in words)
        {
            string temp = EngToHindi(word) + " ";
            ToReturn += temp;
        }
        return ToReturn;
    }
    static string EngToHindi(string EngWord)
    {
        string key1 = EngWord;
        // if not has the meaning return the same word!
        if (!myDictionary.ContainsKey(key1)) return EngWord;
        else return myDictionary[key1];
    }
}
于 2013-08-16T12:10:32.250 回答
-1

这里有一些可能有用的东西......

Dictionary<string, string> EnglishHindiTranslations = null;

public void Populate()
{
    EnglishHindiTranslations = new Dictionary<string, string>();
    EnglishHindiTranslations.Add("my","mera");
    EnglishHindiTranslations.Add("is","hai");
    EnglishHindiTranslations.Add("Name","naam");
}

public string TranslateHindiToEnglish(string hindiWord)
{
    string value = EnglishHindiTranslations.FirstOrDefault(x => x.Value == hindiWord).Key;

    if(string.IsNullOrEmpty(value))
    {
         return hindiWord;
    }

    return value;
 }

public string TranslateEnglishToHindi(string englishWord)
{
    string value = EnglishHindiTranslations.FirstOrDefault(x => x.Key == englishWord).Value;

    if(string.IsNullOrEmpty(value))
    {
         return englishWord;
    }

    return value;
}
于 2013-08-16T12:08:46.973 回答