Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过一些步骤合并和分割两个字符串?_C#_Algorithm_Substring - Fatal编程技术网

C# 如何通过一些步骤合并和分割两个字符串?

C# 如何通过一些步骤合并和分割两个字符串?,c#,algorithm,substring,C#,Algorithm,Substring,我的任务:通过一些步骤合并并分割两个字符串 例如: text=“abcd”key=“12”步骤=2 结果=“1ab2cd” 1)合并字符串 输入变量: text=“abcd”key=“12”步骤=2 输出变量: 结果=“1ab2cd” 2) 现在我需要将字符串的结果解密到源数据,其中包含密钥。已知长度和步长变量 输入变量: 结果=“1ab2cd”键。长度=2步数=2 输出变量: text=“abcd”key=“12” 算法(1)已经实现: int step, word; st

我的任务:通过一些步骤合并并分割两个字符串

例如:
text=“abcd”
key=“12”
步骤=2
结果=“1ab2cd”


1)合并字符串

输入变量:
text=“abcd”
key=“12”
步骤=2

输出变量:
结果=“1ab2cd”

2) 现在我需要将字符串的结果解密到源数据,其中包含密钥。已知长度和步长变量

输入变量:
结果=“1ab2cd”
键。长度=2
步数=2

输出变量:
text=“abcd”
key=“12”


算法(1)已经实现:

    int step, word;
    string text, key, result = string.Empty;

    Console.Write("Enter Text : ");
    text = Console.ReadLine();
    Console.Write("Enter Key : ");
    key = Console.ReadLine();
    Console.Write("Enter Step : ");
    step = int.Parse(Console.ReadLine());

    // MIX ARRAY
    word = 0;

    if (step <= text.Length)
    {
        for (int i = 0; i < key.Length; i++)
        {
            result += key.Substring(i, 1);
            for (int k = 0; k < step; k++)
            {
                try
                {
                    result += text.Substring(word, 1);
                    word++;
                }
                catch
                {
                    break;
                }
            }
        }

        if (word < text.Length)
        {
            result += text.Substring(word, (text.Length - word));
        }
    }
    Console.WriteLine("Result Text : " + result);

    // DECIPHER ARRAY

    Console.WriteLine("text: " + text);
    Console.WriteLine("key: " + key);
    Console.ReadKey();
int步,word;
字符串文本,键,结果=string.Empty;
控制台。写入(“输入文本:”);
text=Console.ReadLine();
控制台。写入(“输入键:”;
key=Console.ReadLine();
控制台。写入(“输入步骤:”;
step=int.Parse(Console.ReadLine());
//混合阵列
字=0;

如果(步骤很抱歉,我无法对此进行测试。我希望这有助于:

     static string Combine(string text, string key, int step)
     {
        var result = "";
        int stepCount = 0;
        for (int i = 0; i < text.Length + key.Length; i++)
        {
            if (i % step == 0)
            {
                result += key[i / step];
                stepCount++;
            }
            else
            {
                result += text[i - stepCount];
            }
        }
        return result;
    }
静态字符串组合(字符串文本、字符串键、int步)
{
var结果=”;
int-stepCount=0;
for(int i=0;i
首先->考虑变量。您需要多少个变量以及哪种类型的变量

    int step,word;
    string text,key,result;
第二->用所需的值填充变量

    Console.Write("Enter Text : ")
    text = Console.ReadLine();
    Console.Write("Enter Key : ")
    key = Console.ReadLine();
    Console.Write("Enter Step : ")
    step = int.Parse(Console.ReadLine());
第三->创建解决此问题的算法

第四->思考什么样的例外情况以及如何解决它们

    for(int i=0;i<key.Length;i++)
    {
        result+=key.Substring(i,1);
        for(int k=0; k<step; k++)
        {
           try
           {
               result+=text.Substring(word,1);
               word++;
           }
           catch
           {
               /* This blok will break 
                  when the text variable's last part's character count lest then step. */
               break;
           }
        }
     }
        if(word < text.Length)
        {
             // if there is any text after all. Calculate how many letter left then write them
             result += text.Substring(word,(text.Length-word))
        }

Console.Write("Result Text : "+result);
Console.ReadKey();

for(inti=0;i我将把它写成另一个代码块,如果需要,您可以合并2个algorthym。
再想想我的4个步骤

1-找到您需要的变量

        string key = "", text = "", result;
        int step = 0, keyLength, textLength, word = 0;
2-取所有值

        Console.Write("Result Text : ");
        result = Console.ReadLine();
        Console.Write("Key Length: ");
        keyLength = int.Parse(Console.ReadLine());
        textLength = result.Length - keyLength;
        Console.Write("Step: ");
        step = int.Parse(Console.ReadLine());
3-想想算法

4-考虑可能出现的例外情况

       for (int i = 0; i < result.Length; i = word)
        {
            if (keyLength > 0)
            {
                key += result.Substring(word, 1);
                word++;
                keyLength--;
            }
            if (textLength > 0)
            {
                for (int k = 0; k < step; k++)
                {
                    try
                    {
                        text += result.Substring(word, 1);
                        word++;
                        textLength--;
                    }
                    catch
                    {
                        break;
                    }
                }
            }

        }
        Console.WriteLine("Text : " + text);
        Console.Write("Key : " + key);
        Console.ReadKey();
for(int i=0;i0)
{
关键字+=结果子字符串(字,1);
word++;
键长--;
}
如果(文本长度>0)
{
对于(int k=0;k
对于哪些输入不起作用?得到的结果是什么?@GiladGreen text=“abcde”;key=“123”;step=1;result=“1a2b3c”如果step=2,则错误:索引超出了数组的边界。如何以相反的方向实现此算法?例如:将“1ab2cd”解码为变量:text=“abcd”和key=“12”(已知键长和步长变量)如果你想给出一个像ab1cd2或a12bcd这样的结果,你必须改变一些东西如果你想ab1cd2,你必须改变步骤的位置,它有点复杂,时间是5a.m如果你想,我可以明天写,第二个选择,你可以简单地用切换键和文本变量places on for loop来做。你在哪里看到键放在那里文本和whe你看到文本也放在那里了吗
       for (int i = 0; i < result.Length; i = word)
        {
            if (keyLength > 0)
            {
                key += result.Substring(word, 1);
                word++;
                keyLength--;
            }
            if (textLength > 0)
            {
                for (int k = 0; k < step; k++)
                {
                    try
                    {
                        text += result.Substring(word, 1);
                        word++;
                        textLength--;
                    }
                    catch
                    {
                        break;
                    }
                }
            }

        }
        Console.WriteLine("Text : " + text);
        Console.Write("Key : " + key);
        Console.ReadKey();