[英]Assign new value to a string index
我需要將用戶輸入字符串中的字符a
, e
, i
, o
, u
轉換為指定的符號。 以下是我到目前為止的情況。
作業
首先提示用戶輸入加密的文本字符串。 驗證這不是空白。 將此文本字符串發送到您將創建的將自動解密的自定義方法。 解密后,將此文本字符串返回到main,您將輸出加密和解密的字符串。
要解密文本字符串,您必須執行以下字符替換:
@
= a
#
= e
^
= i
*
= o
+
= u
我目前的代碼
public static string Decipher (string code)
{
char[] array = code.ToCharArray();
for (int i = 0; i < code.Length; i++)
{
if (code.Contains("@") && code.Contains("#") && code.Contains("^") &&
code.Contains("*") && code.Contains("+"))
{
}
}
每次執行此for循環時,如果字符串在字符串中的任何位置包含@
, #
, ^
, *
和 +
,則它將評估為true。 因此,如果您的字符串缺少任何這些符號, if
語句將評估為false
並且不會發生任何事情。
幸運的是,你可以很容易地簡化這個。 一種方法是將string
轉換為char[]
數組,並將邏輯分解為多個if
- else
語句或單個switch
語句,例如:
public static string Decipher (string code)
{
char[] codeArray = code.ToCharArray(); // convert your code string to a char[] array
for (int i = 0; i < codeArray.Length; i++)
{
switch (codeArray[i]) // Get the char at position i in the array
{
case '@': // if the character at i is '@'
codeArray[i] = 'a'; // Change the character at i to 'a'
break; // break out of the switch statement - we don't need to evaluate anything else
case '#': // if the character at i is '#'
codeArray[i] = 'e'; // Change the character at i to 'e'
break; // break out of the switch statement - we don't need to evaluate anything else
// repeat for everything else you need to replace!
}
}
return new String(codeArray); // Once you're all done, create a string from your deciphered array and return it
}
有很多不同的方法可以做到這一點。 在循環中進行字符串連接(如@Acex所示)通常不贊成; 它會消除很多“垃圾”,並可能減慢速度。 Stringbuilder
類通常是更好的選擇。 我的代碼使用Stringbuilders(好吧,一遍又一遍 - 我在兩者之間清除它)。
以下是做同樣事情的幾種方法:
const string encoded = "H#ll*, H*w @r# y*+?";
//good old fashioned C-style/brute force:
var buf = new StringBuilder();
foreach (var c in encoded){
switch(c){
case '@':
buf.Append('a');
break;
case '#':
buf.Append('e');
break;
case '^':
buf.Append('i');
break;
case '*':
buf.Append('o');
break;
case '+':
buf.Append('u');
break;
default:
buf.Append(c);
break;
}
}
var result = buf.ToString();
//using a lookup table (easily extensible)
buf.Clear();
var decodeDict = new Dictionary<char, char>{
{'@', 'a'},
{'#', 'e'},
{'^', 'i'},
{'*', 'o'},
{'+', 'u'},
};
foreach (var c in encoded){
if (decodeDict.Keys.Contains(c)){
buf.Append(decodeDict[c]);
} else {
buf.Append(c);
}
}
result = buf.ToString();
//something completely different
//instead of iterating through the string, iterate through the decoding dictionary
buf.Clear();
var working = new StringBuilder(encoded.Length);
working.Append(encoded);
foreach (var pair in decodeDict){
working.Replace(pair.Key, pair.Value);
}
result = working.ToString();
在每種情況下, result
保持結果。 在每次結果分配后立即設置一個斷點,看看發生了什么。
我沒有提供很多評論,逐步完成代碼,查找課程並弄清楚我在做什么(畢竟你是學生)。
這真的很簡單:
public static string Decipher(string code)
{
var map = new Dictionary<char, char>()
{
{ '@', 'a' },
{ '#', 'e' },
{ '^', 'i' },
{ '*', 'o' },
{ '+', 'u' },
};
char[] array = code.ToCharArray();
array = array.Select(x => map.ContainsKey(x) ? map[x] : x).ToArray();
return new string(array);
}
現在你可以這樣做:
string cipher = "*+tp+t";
string plain = Decipher(cipher);
Console.WriteLine(cipher);
Console.WriteLine(plain);
這輸出:
*+tp+t output
為了替代
並且當您被允許使用現有方法時。
你可以寫這樣的東西:
private Dictionary<char, char> mapping = new Dictionary<char, char>()
{
{ '@', 'a' },
{ '#', 'e' },
{ '^', 'i' },
{ '*', 'o' },
{ '+', 'u' },
};
private string Decrypt(string encryptedString) =>
string.Concat(encryptedString.Select(s => mapping.ContainsKey(s) ? mapping[s] : s));
用法:
string result = Decrypt(encryptedString);
參考文獻: DotNetFiddle示例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.