For example i have
String Text = "ABCDEFGHIJKLMNOPQ";
to run through some codes to get into
String[] text1 = "AOCN";
String[] text2 = "JQBF";
String[] text3 = "DMG";
String[] text4 = "HPI";
String[] text5 = "KEL";
then some code to get it back to
String Text = "ABCDEFGHIJKLMNOPQ";
is this possible?
what I am trying to achieve is random storing the spilt character into 5 different String Array and use code to revert it back to it original text
Assuming that the request for a "random" allocation of letters to arrays is for a pseudo-random (or, perhaps, a superficially arbitrary) allocation that is therefore reversible, one technique to do this would be to essentially use a transposition cipher.
The algorithm would then be something like:
- Run the transposition cipher on the input text.
- Split the transposed text into the arrays.
The original text would then be obtained by reversing the two steps.
(EDIT)
The transposition cipher key could consist of a stream of pseudo-random numbers from 1
to n
where n
is the number of strings into which the input string is to be split. Thus, an expanded algorithm would read as follows:
- Generate a list of pseudo-random numbers,
p
, of length m
, where m
is the length of the input string.
- For all
i
, assign the i
th letter in the input string to the output string number p[i]
.
To reassemble the original string:
- For all
i
, obtain the i
th letter in the string from the next unused letter in string number p[i]
.
For an arbitrary string, and assuming your distribution is truly random, then unless you somehow stored the randomizing factors there would be no way to reassemble the original string. This reminds me of Write-Only-Memory.
try this:
static void Main(string[] args)
{
string Text = "ABCDEFGHIJKLMNOPQ";
int chunk = new Random().Next(1,Text.Length/2);
var result= Split(Text,chunk);
Console.WriteLine("Splited:");
foreach (var word in result)
{
Console.WriteLine(word);
}
string toOld = "";
Console.WriteLine("Returned:");
toOld = result.Aggregate((i, j) => i + j);
Console.WriteLine(toOld);
Console.ReadLine();
}
static List<string> Split(string str, int chunkSize)
{
var re = Enumerable.Range(0, str.Length / chunkSize)
.Select(i => str.Substring(i * chunkSize, chunkSize)).ToList() ;
var temp = re.Aggregate((i, j) => i + j);
if (temp.Length < str.Length)
{
var last = re.Last();
last += str.Substring(temp.Length, str.Length - temp.Length);
re[re.Count-1] = last;
}
return re;
}
you can control chunk size
Yes you can iterate character by character :
using System;
class Program
{
static void Main()
{
const string s = "Hello!";
// Option 1
foreach (char c in s)
{
Console.WriteLine(c); // Your treatment here
}
// Option 2
for (int i = 0; i < s.Length; i++)
{
Console.WriteLine(s[i]); // Your treatment here
}
}
}
You can use this to concatenate (treatment porcess) :
if (some_condition) text1 += s[i];
Then in Your treatment here
parts you could use basic random functions provided by C#. As long as you do not change the seed
, you can retrieve the sequence used to generate the substrings and probably revert it...
Could be, by example, something like that :
int seed = 12;
List<int> lst = new List<int>();
// Repeat that until you processed the whole string
// At the mean time, skip the characters already indexed
while (lst.Count != s.Length) {
int n = new Random(seed).Next(0, s.Length);
if (!lst.Contains(n)) {
text1 += s[n];
lst.Add(n);
}
}
In the end lst
is your key to revert the process.
Then the way you generate the substring, and the algorithm to restore the original string is up to you too... You are completely free.
Note : Concerning dealing with chunks
, please refer to Simon
's answer.