Reputation: 47
My task: Merge and divide two strings with some step.
For example:
text = "abcd"
key = "12"
step = 2
result = "1ab2cd"
Пример:
1) Merging strings.
Input variables:
text = "abcd"
key = "12"
step = 2
Output variables:
result = "1ab2cd"
2) Now I need to decrypt the result of string to the source data, where key. Length and step variables are known.
Input variables:
result = "1ab2cd"
key.Length = 2
step = 2
Output variables:
text = "abcd"
key = "12"
Algorithm (1) has already been implemented:
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();
How to implement the algorithm (2)?
Upvotes: 1
Views: 178
Reputation: 248
I will write this as another code block you can merge 2 algorthym if you want. Again Think with my 4 steps.
1- Find what variables do you need?
string key = "", text = "", result;
int step = 0, keyLength, textLength, word = 0;
2- Take All Values
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- Think about algorthym
4- Think about that may arise exceptions
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();
Upvotes: 1
Reputation: 248
text = "abcd" key = "12" step = 2 result = "1ab2cd"
First ->Think about variables. How many and which kind of variable are your need.
int step,word;
string text,key,result;
Second ->Fill variables with needed values
Console.Write("Enter Text : ")
text = Console.ReadLine();
Console.Write("Enter Key : ")
key = Console.ReadLine();
Console.Write("Enter Step : ")
step = int.Parse(Console.ReadLine());
Third ->Create an algorthym for solve this problem
Forth ->Think about what exception can be and how can you solve them
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();
I hope this can help to you
Upvotes: 1
Reputation: 1241
I'm sorry I can't test this. I hope it helps:
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;
}
Upvotes: 0