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

时间:2017-11-18 11:13:46

标签: c# algorithm substring

我的任务:合并并分割两个字符串。

  

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

Пример:
1)合并字符串。

  

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

     

输出变量:
  result =“1ab2cd”

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

  

输入变量:
  result =“1ab2cd”
key.Length = 2
step = 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();

如何实现算法(2)?

3 个答案:

答案 0 :(得分:1)

text = "abcd" 
key = "12" 
step = 2 
result = "1ab2cd"

首先 - &gt;考虑变量。你需要多少和哪种变量。

    int step,word;
    string text,key,result;

第二 - &gt;用所需值填充变量

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

第三 - &gt;创建一个用于解决此问题的算法

Forth - &gt;考虑什么是异常以及如何解决它们

    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();

我希望这可以对你有所帮助

答案 1 :(得分:1)

如果你愿意的话,我会把它写成另一个代码块你可以合并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-想想algorthym

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();

答案 2 :(得分: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;
    }