C# 将字符串数组转换为字符串
我正在开发一个密码程序,它使用一个柱状转置密码。我首先抓取用户输入并将其放入一个字符串数组,然后打印一个无序版本的数组。要破译消息,我将取消填充加密消息,并尝试从字符串数组中的每个“列”中打印一个元素 例如,如果我的消息是“Hey there dude”,它将被加密为“TD*HU*ERE\uuuu*YE*HED”(空格替换为下划线,空空格替换为星号)。然后,解密应该重新组织数组,然后遍历数组中每个元素的第一项,将其打印到字符串,然后移动到数组中每个元素的第二项,将其打印到字符串,依此类推C# 将字符串数组转换为字符串,c#,arrays,string,C#,Arrays,String,我正在开发一个密码程序,它使用一个柱状转置密码。我首先抓取用户输入并将其放入一个字符串数组,然后打印一个无序版本的数组。要破译消息,我将取消填充加密消息,并尝试从字符串数组中的每个“列”中打印一个元素 例如,如果我的消息是“Hey there dude”,它将被加密为“TD*HU*ERE\uuuu*YE*HED”(空格替换为下划线,空空格替换为星号)。然后,解密应该重新组织数组,然后遍历数组中每个元素的第一项,将其打印到字符串,然后移动到数组中每个元素的第二项,将其打印到字符串,依此类推
string[] columnEncrypted = userMessage.Split(' ');
string[] columnDecrypted = new string[6];
string DecryptedString = string.Empty;
columnDecrypted[0] = columnEncrypted[5];
columnDecrypted[1] = columnEncrypted[2];
columnDecrypted[2] = columnEncrypted[4];
columnDecrypted[3] = columnEncrypted[3];
columnDecrypted[4] = columnEncrypted[0];
columnDecrypted[5] = columnEncrypted[1];
for (int r = 0; r <= columnDecrypted.Length; r++)
{
for (int c = 0; c < 6; c++)
{
if (columnDecrypted[c] == "_")
{
DecryptedString += ' ';
}
else
{
DecryptedString += columnDecrypted[c];
}
}
}
return DecryptedString;
string[]columnEncrypted=userMessage.Split(“”);
string[]columnDecrypted=新字符串[6];
string DecryptedString=string.Empty;
columnDecrypted[0]=columnEncrypted[5];
columnDecrypted[1]=columnEncrypted[2];
columnDecrypted[2]=columnEncrypted[4];
columnDecrypted[3]=columnEncrypted[3];
columnDecrypted[4]=columnEncrypted[0];
columnDecrypted[5]=columnEncrypted[1];
对于(int r=0;r这就是我最终解决问题的原因:
public string CipheredString(string userMessage)
{
string[] columnEncrypted = userMessage.Split(' ');
string[] columnDecrypted = new string[6];
string DecryptedString = string.Empty;
string DirtyDecrypt = string.Empty;
columnDecrypted[0] = columnEncrypted[5];
columnDecrypted[1] = columnEncrypted[2];
columnDecrypted[2] = columnEncrypted[4];
columnDecrypted[3] = columnEncrypted[3];
columnDecrypted[4] = columnEncrypted[0];
columnDecrypted[5] = columnEncrypted[1];
for (int c = 0; c < columnDecrypted[0].Length; c++)
{
for (int r = 0; r < 6; r++)
{
string row = columnDecrypted[r];
string column = row[c].ToString();
DirtyDecrypt += column;
}
}
string CleanDecrypt = DirtyDecrypt.Replace("_", " ").Replace("*","");
return CleanDecrypt;
}
公共字符串加密字符串(字符串用户消息)
{
string[]columnEncrypted=userMessage.Split(“”);
string[]columnDecrypted=新字符串[6];
string DecryptedString=string.Empty;
string DirtyDecrypt=string.Empty;
columnDecrypted[0]=columnEncrypted[5];
columnDecrypted[1]=columnEncrypted[2];
columnDecrypted[2]=columnEncrypted[4];
columnDecrypted[3]=columnEncrypted[3];
columnDecrypted[4]=columnEncrypted[0];
columnDecrypted[5]=columnEncrypted[1];
for(int c=0;c
hmm…问题是什么?你在描述你的程序应该做什么方面做得很好,但我想你忘了解释你的问题了。:)为了让我们回答你的问题,你需要告诉我们你的问题。我需要将解密的字符串打印出来“嘿,你在那里,哥们****”。现在,它正在打印更多类似于“HEDEREYE*TDHUHEDEREYETDHUHEDEREYETDHUHEDEREYETDHU*”的内容,VS附带了一个强大的调试器。使用它并学习。
public string CipheredString(string userMessage)
{
string[] columnEncrypted = userMessage.Split(' ');
string[] columnDecrypted = new string[6];
string DecryptedString = string.Empty;
string DirtyDecrypt = string.Empty;
columnDecrypted[0] = columnEncrypted[5];
columnDecrypted[1] = columnEncrypted[2];
columnDecrypted[2] = columnEncrypted[4];
columnDecrypted[3] = columnEncrypted[3];
columnDecrypted[4] = columnEncrypted[0];
columnDecrypted[5] = columnEncrypted[1];
for (int c = 0; c < columnDecrypted[0].Length; c++)
{
for (int r = 0; r < 6; r++)
{
string row = columnDecrypted[r];
string column = row[c].ToString();
DirtyDecrypt += column;
}
}
string CleanDecrypt = DirtyDecrypt.Replace("_", " ").Replace("*","");
return CleanDecrypt;
}