Rigel Johnston
Rigel Johnston

Reputation: 49

Finding the index of chars that repeats

I'm really bad at explaining things, but I'll try my best.

I'm making a small program that converts one word into another as you type. Each letter that is typed goes through this section of code where it is changed to a different letter depending on its Index position of the whole word.

My issue here is that when there are repeating letters, the letters that repeat don't change according to their position within the word but rather the first occurrence.

For example this made up word "bacca". If you put that through the code, it SHOULD change to "vrwiy" but instead it changes to "vrwwr". I know why this is too. It's because the switch statement loops through the word that needs to be converted. However I'm without a clue on how to make it change each char according to it's own individual position within the index of the string. I thought maybe the LastIndexOf() method would work but instead it just reverses the order. So if I were to type the letter "a", it would come out as "n", but if I were to type "aa", it would switch the first "a" to "r" because the second is at the IndexOf 1 get's changed to "r".

private void inputTbox_TextChanged(object sender, EventArgs e)
{
    List<string> rawZnWordList = new List<string>();
    foreach (char a in inputTextBox.Text) 
    {
        switch (inputTextBox.Text.IndexOf(a)) 
        {
            case 0: 
                switch (a) 
                {
                    case 'a':
                        rawZnWordList.Add("n");
                        continue;
                    case 'b':
                        rawZnWordList.Add("v");
                        continue;
                    case 'c':
                        rawZnWordList.Add("a");
                        continue;
                    default:
                        break;
                }

                continue;
            case 1:
                switch (a)
                {
                    case 'a':
                        rawZnWordList.Add("r");
                        continue;
                    case 'b':
                        rawZnWordList.Add("x");
                        continue;
                    case 'c':
                        rawZnWordList.Add("z");
                        continue;
                    default:
                        break;
                }
                continue;
            case 2:
                switch (a)
                {
                    case 'a':
                        rawZnWordList.Add("t");
                        continue;
                    case 'b':
                        rawZnWordList.Add("l");
                        continue;
                    case 'c':
                        rawZnWordList.Add("w");
                        continue;
                    default:
                        continue;
                }
                continue;
            case 3:
                switch (a) 
                {
                    case 'a':
                        rawZnWordList.Add("u");
                        continue;
                    case 'b':
                        rawZnWordList.Add("i");
                        continue;
                    case 'c':
                        rawZnWordList.Add("o");
                        continue;
                    default:
                        break;
                }
                continue;
            case 4:
                switch (a)
                {
                    case 'a':
                        rawZnWordList.Add("y");
                        continue;
                    case 'b':
                        rawZnWordList.Add("m");
                        continue;
                    case 'c':
                        rawZnWordList.Add("d");
                        continue;
                    default:
                        break;
                }
                continue;
            default:
                break;
        }
    }
    string finalZnWord = string.Join("", rawZnWordList.ToArray()); 
    outputTextBox.Text = finalZnWord;
}

Upvotes: 1

Views: 119

Answers (3)

Roy Ashbrook
Roy Ashbrook

Reputation: 854

I think this does the same thing and is a lot more readable. Of course replace the letter rings with your own values. I only went up to 5 characters. I'm guessing you would want more.

//replacement letter rings
char[][] aRep = { 
    "xfhygaodsekzcpubitlvnjqmrw".ToCharArray(),
    "wqtnsepkbalmzyxvordhjgifcu".ToCharArray(),
    "nyxgmcibplovkwrszaehftqjud".ToCharArray(),
    "soqjhpybuwfxvartkzginemdcl".ToCharArray(),
    "pldquhegkaomcnjrfxiysvtbwz".ToCharArray(),
};
private string newText(string inVal)
{
    char[] ia = inVal.ToCharArray(); //in array
    int l = ia.Length;
    char[] oa = new char[l]; //out array
    for (int i = 0; i < l; i++)
        oa[i] = aRep[i][(int)ia[i]-97]; //lowercase starts at char97
    return new string(oa);
}

Here is also the code I used to generate the rings:

//generate random letter rings
//char[] ascii = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
//for (int i = 0; i < 8; i++)
//  new string(ascii.OrderBy (x => Guid.NewGuid() ).ToArray()).Dump();

I did some very simple testing and this seemed quite a bit faster than the original approach and more importantly (to me) it is way more readable and a lot easier to see your replacement letter sequences. I think there is some more optimization to be done, but this i think is a good start. Just call this with your text during 'on change'.

Upvotes: 0

Aaron D
Aaron D

Reputation: 5876

You need to keep track of the index inside your foreach instead of using .IndexOf. You could also use a regular for loop instead of a foreach but this way is a minimal change to your code.

private void inputTbox_TextChanged(object sender, EventArgs e)
{
    List rawZnWordList = new List();
    int index = 0;
    foreach (char a in inputTextBox.Text) 
    {
        switch (index) 
        {
            case 0: 
                switch (a) 
                {
                    case 'a':
                        rawZnWordList.Add("n");
                        continue;
                    case 'b':
                        rawZnWordList.Add("v");
                        continue;
                    case 'c':
                        rawZnWordList.Add("a");
                        continue;
                    default:
                        break;
                }
                continue;
            case 1:
                switch (a)
                {
                    case 'a':
                        rawZnWordList.Add("r");
                        continue;
                    case 'b':
                        rawZnWordList.Add("x");
                        continue;
                    case 'c':
                        rawZnWordList.Add("z");
                        continue;
                    default:
                        break;
                }
                continue;
            case 2:
                switch (a)
                {
                    case 'a':
                        rawZnWordList.Add("t");
                        continue;
                    case 'b':
                        rawZnWordList.Add("l");
                        continue;
                    case 'c':
                        rawZnWordList.Add("w");
                        continue;
                    default:
                        continue;
                }
                continue;
            case 3:
                switch (a) 
                {
                    case 'a':
                        rawZnWordList.Add("u");
                        continue;
                    case 'b':
                        rawZnWordList.Add("i");
                        continue;
                    case 'c':
                        rawZnWordList.Add("o");
                        continue;
                    default:
                        break;
                }
                continue;
            case 4:
                switch (a)
                {
                    case 'a':
                        rawZnWordList.Add("y");
                        continue;
                    case 'b':
                        rawZnWordList.Add("m");
                        continue;
                    case 'c':
                        rawZnWordList.Add("d");
                        continue;
                    default:
                        break;
                }
                continue;
            default:
                break;
        }
        index++;
    }
    string finalZnWord = string.Join("", rawZnWordList.ToArray()); 
    outputTextBox.Text = finalZnWord;
}

Upvotes: 0

educampver
educampver

Reputation: 3005

You should try using a for loop instead, like this:

for (int i = 0; i < inputTextBox.Text.Length; i++) 
    {
        char a = inputTextBox.Text[i];
        switch (i) 
        {
            case 0: 
                switch (a) 
                ...

Hope this helps ;).

Upvotes: 1

Related Questions