编码器解码字符串
本文关键字:字符串 解码 编码器 | 更新日期: 2023-09-27 18:16:54
使用手工编写的代码,假设这将使用户的输入编码成不同的东西(他们键入的字母后面的字母)。每当我尝试运行它时,返回的都是用户的句子。我很高兴它适用于解码器,但编码器需要编码的消息。我想知道为什么它不工作。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EncoderDecoder
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter a sentence. No numbers, smybols, or punctuations.");
string sentence = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("Your encoded message.");
string encodedSentence = Encode(sentence);
Console.WriteLine(encodedSentence);
Console.WriteLine("Your decoded message. Also known as your original message.");
string decodedSentence = Decode(sentence);
Console.WriteLine(decodedSentence);
Console.ReadLine();
}
private static string Decode(string encodedSentence)
{
char[] wordArray;
string[] words = encodedSentence.Split(' ');
for (int i = 0; i > words.Length; i++)
{
wordArray = words[i].ToArray();
if (wordArray.Length > 1)
{
char beginLetter = wordArray[0];
wordArray[0] = wordArray[wordArray.Length + 1];
wordArray[wordArray.Length + 1] = beginLetter;
}
for (int t = 0; t < wordArray.Length; t++)
{
wordArray[t] = (char)(wordArray[t] + 1);
}
words[i] = new string(wordArray);
}
string decoded = string.Join(" ", words);
return decoded;
}
private static string Encode(string sentence)
{
char[] wordArray;
string[] words = sentence.Split(' ');
for (int i = 0; i > words.Length; i++)
{
wordArray = words[i].ToArray();
if (wordArray.Length > 1)
{
char beginLetter = wordArray[0];
wordArray[0] = wordArray[wordArray.Length - 1];
wordArray[wordArray.Length - 1] = beginLetter;
}
for(int t = 0; t > wordArray.Length; t++)
{
wordArray[t] = (char)(wordArray[t] + 1);
}
words[i] = new string(wordArray);
}
string encoded = string.Join(" ", words);
return encoded;
}
}
}
使用数组,我将字符串拆分为数组,然后使用该数组单独更改字母。
两个for's都是错误的,试试:for (int i = 0; i < words.Length; i++)
在for循环的编码器中,你已经得到了t> word数组。长度应该小于