2016-01-07 107 views
1

嗨,我正在做班拉丁拉丁語,說明是第一個輔音是從字的前面刪除,並放在單詞的背面。然後是字母「ay」。例子是,書成爲ookbay,並且力量變得吞噬。我有麻煩,因爲它沒有做第一個輔音。豬拉丁語控制檯

// button, three, nix, eagle, and troubadour 
Console.Write("Enter word you want in Pig Latin: "); 
string word1 = Console.ReadLine(); 
string pig = ""; 
string vowels = "aeiouAEIOU"; 
string space = " "; 
string extra = ""; //extra letters 
int pos = 0; //position 

foreach (string word in word1.Split()) 
{ 
    if (pos != 0) 
    { 
     pig = pig + space; 
    } 
    else 
    { 
     pos = 1; 
    } 

    vowels = word.Substring(0,1); 
    extra = word.Substring(1, word.Length - 1); 
    pig = pig + extra + vowels + "ay"; 
} 

Console.WriteLine(pig.ToString()); 

例如,如果我做力量將拿出作爲trengthsay和不喜歡的例子

+0

您正在定義頂部附近的「元音」,然後再覆蓋它。至於你的問題,我建議搜索第一個元音的索引,然後使用它作爲第一個'substring'調用的第二個參數(它目前只抓取第一個字符) – Krease

+0

也可以查看[子(INT)](https://msdn.microsoft.com/en-us/library/hxthx5h6(v = vs.110)的.aspx)。你不需要指定你想從'pos'到'word.Length-1',你可以簡單地寫'word.Substring(pos);' – Ian

回答

3

你有一些問題存在。首先,你的問題的定義:

的指示是第一個輔音從字

的前去掉這正是你做了什麼。 strength確實變成trengths如果您移動第一個輔音。您需要將您的定義更改爲所有主導輔音直到第一個元音。另外,你在eagle的情況下做什麼?它成爲eagleay?您的指示並未指定如何處理主導元音。

這是另一個問題

vowels = word.Substring(0,1); // This will overwrite your vowel array with the first letter 

不要擔心編寫實際的代碼,只是還沒有,寫一些僞代碼先制定出你的邏輯。 @克里斯關於尋找第一個元音的評論是一個好主意。您的僞代碼可能如下所示:

Check if word begins with consonant 
{ 
    If so, look for index of first vowel 
    Take substring of word up to first vowel. 
    Append it to end 
} 
Otherwise 
{ 
    Deal with leading vowel 
} 
+1

僞代碼在這裏很少使用 –

+1

對於所有的編碼器,但特別是初學者,從代碼中退一步,看看總體目標是非常有幫助的。很多時候,我發現自己停留在特定的代碼行,然後意識到我從一開始就以錯誤的方式攻擊它。 – Ian

+0

經過這麼多年,它只是直觀地給我;)但是你說得對,這對於新手來說是一個很好的方法(谷歌僞代碼片段) –