I am trying to create a program which will substitute a character in place of another. I have created two arrays, odd and even as follows:
char[] odd = { 'a', 'c', 'e', 'g', 'i', 'k', 'm', 'o', 'q', 's', 'u', 'w', 'y' };
char[] even ={ 'b', 'd', 'f', 'h', 'j', 'l', 'n', 'p', 'r', 't', 'v', 'x', 'z' };
Now, if the input is 'a', it has to put 'b' and if the input is 'b', it has to print 'a'.
So i thought it is best to match the index of both the arrays since they have equal count. the basic idea is this :
the input is 'a', so the compiler takes the index of 'a' - 0 into account and substitutes the value at even with the index 0 - 'b'.
so I thought of assigning a spare character 'j', which will find the index of the input and outputs the corresponding character from the array - even[]
.
I tried few times but could not get a solution. Kindly help me in this...
This is what I have tried.
The desired input maybe like this : Hello The desired output should be this: Gfkkp
This supposedly gives me the desired result.
private void superSecretFormula(string myName)
{
string read = myName;
int count = read.Length;
char[] odd = { 'a', 'c', 'e', 'g', 'i', 'k', 'm', 'o', 'q', 's', 'u', 'w', 'y' };
//char[] j;
char[] even = { 'b', 'd', 'f', 'h', 'j', 'l', 'n', 'p', 'r', 't', 'v', 'x', 'z' };
for (int i = 0; i < count; i++)
{
for (int j = 0; j < 13 ; j++)
{
if (read[i]==odd[j])
{
int k = j;
textBlock1.Text = textBlock1.Text + Convert.ToString(char.ToUpperInvariant(even[k]));
}
if (read[i] == even[j])
{
int k = j;
textBlock1.Text = textBlock1.Text + Convert.ToString(char.ToUpperInvariant(odd[k]));
}
}
}
}
Kindly comment on the quality and approach in solving this problem, as I am a newbie and I am starting to learn code only now. Thanks