0

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

4

4 回答 4

4

假设您所有的替换都是独一无二的,我会考虑将您的结构更改为这样的结构以简化使用。

class Program
{
    static void Main(string[] args)
    {
        var subs = new Substitutes {{'a', 'b'}, {'c', 'd'}};

        Console.WriteLine(subs['a']); //Prints b
        Console.WriteLine(subs['b']); //Prints a
    }

    class Substitutes : Dictionary<char, char>
    {
         public new void Add(char item, char substitute)
         {
             base.Add(item, substitute);
             base.Add(substitute, item);
         }
    }
}

现在只需检查键是否存在,替换字符串中的所有字符就很简单了。

于 2012-06-24T16:16:08.687 回答
0

一种方法是创建一个类,该类包含对包含应转置字母的两个字典的引用;类似于OP的原始两个数组。然后创建一个方法来转换输入字符串:

public class TransposeLetters
{
    public Dictionary<char, char> Odd { get; private set; }
    public Dictionary<char, char> Even { get; private set; }

    public TransposeLetters()
    {
        Odd = new Dictionary<char, char>();

        // capital leters
        for( int i = 65; i <= 90; i += 2 )
        {
            Odd.Add( (char)i, (char)( i + 1 ) );
        }

        // small letters
        for( int i = 97; i <= 122; i += 2 )
        {
            Odd.Add( (char)i, (char)( i + 1 ) );
        }

        Even = Odd.ToDictionary( x => x.Value, x => x.Key );
    }

    public string Convert( string str )
    {
        // ensure only letters are passed into the method
        var cleansed = str.Where( char.IsLetter );

        var transposed = cleansed.Select( x => Odd.ContainsKey( x ) ? Odd[x] : Even[x] );

        var result = new string( transposed.ToArray() );

        return result;
    }
}
于 2012-06-24T18:13:24.170 回答
0

以最少的修改坚持您的原始想法。

Array.IndexOf(odd, 'a'); //will return 0
Array.IndexOf(odd, 'b')' //will return -1

所以....

if(Array.IndexOf(odd, inputChar) > -1 )
{
    Console.WriteLine(even[Array.IndexOf(odd, inputChar)]);
}
else
{
    Console.WriteLine(odd[Array.IndexOf(even, inputChar)]);
}

文档:http: //msdn.microsoft.com/en-us/library/eha9t187.aspx

于 2012-06-24T16:28:16.550 回答
0

我不知道你在哪里需要帮助。一个小函数替换odd相应字符串中包含的字符串的每个字符,even反之亦然,如下所示:

static char[] odd = { 'a', 'c', 'e', 'g', 'i', 'k', 'm', 'o', 'q', 's', 'u', 'w', 'y' };
static char[] even = { 'b', 'd', 'f', 'h', 'j', 'l', 'n', 'p', 'r', 't', 'v', 'x', 'z' };

static string Substitute(string input)
{
    char[] result = new char[input.Length];
    int i = 0;
    bool found = false;
    foreach (char c in input.ToCharArray())
    {
        found = false;
        result[i] = c;
        for (int j = 0; j < odd.Length; j++)
        {
            if (odd[j] == c)
            {
                result[i] = even[j];
                found = true;
                break;
            }
        }

        if (!found)
        {
            for (int j = 0; j < even.Length; j++)
            {
                if (even[j] == c)
                {
                    result[i] = odd[j];
                    break;
                }
            }
        }
        i++;
    }
    return new string(result);
}

您还可以通过创建从每个输入字符到输出字符的某种映射来优化这一点,该映射只需要创建一次然后被缓存:

static char[] odd = { 'a', 'c', 'e', 'g', 'i', 'k', 'm', 'o', 'q', 's', 'u', 'w', 'y' };
static char[] even = { 'b', 'd', 'f', 'h', 'j', 'l', 'n', 'p', 'r', 't', 'v', 'x', 'z' };
static int[] mapping = null;

static string Substitute(string input)
{
    if (mapping == null)
    {
        mapping = new int[Math.Max(odd.Select(x => (int)x).Max(), even.Select(x => (int)x).Max()) + 1];

        for (int i = 0; i < mapping.Length; i++) mapping[i] = i;

        // Note: Make sure 'odd' and 'even' are arrays of the same length
        for (int i = 0; i < odd.Length; i++) mapping[(int)odd[i]] = (int)even[i];
        for (int i = 0; i < even.Length; i++) mapping[(int)even[i]] = (int)odd[i];
    }

    char[] result = new char[input.Length];
    int j = 0;
    foreach (char c in input.ToCharArray())
    {
        int charcode = (int)c;
        result[j] = charcode >= mapping.Length ? c : (char)mapping[charcode];
        j++;
    }
    return new string(result);
}
于 2012-06-24T16:03:15.093 回答