armadana
armadana

Reputation: 21

matching an array with another array

i'm looking for an array matching method.

here i have two arrays as the code shows

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

and, i want to match them so, if i write "abc" in the program it will turn into "bcd" and, i've made a text parser method like this:

        parsing = input.ToCharArray();
        foreach (char c in parsing)
        {
            throw new NotImplementedException();
        }

but, i don't know what kind of query should i do to match them after the foreach statement. if you know how to match this in code, please post here, it would be VERY2 APPRECIATED

Upvotes: 2

Views: 307

Answers (4)

Aleksandar Grgic
Aleksandar Grgic

Reputation: 41

Something like this, now format it to a way best suited to you.

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

parsing = input.ToCharArray();  
foreach (char c in parsing)
        {
           if(index(c,normalText)<= dictionary.Length) 
               Console.WriteLine(dictionary[index(c,normalText)]);
        }

int index(char lookFor, char[] lookIn) 
    {
        for (int i = 0; i < lookIn.Length; i++)
            {
                if (lookIn[i] == lookFor)
                    return i;
            }
        return -1;
    }

Upvotes: 0

J...
J...

Reputation: 31403

It looks like you want to use these to make for a 1:1 translation.

The best (ie:most extensible) way to do this is probably with a dictionary :

Dictionary<char, char> dictionary = new Dictionary<char, char>();
dictionary.Add('a', 'b');
dictionary.Add('b', 'c');
dictionary.Add('c', 'd');
//... etc (can do this programmatically, also

then :

char newValue = dictionary['a'];
Console.WriteLine(newValue.ToString()); // "b"

and so on. Using a dictionary you get all the power of lists, as well, which can be immensely handy depending on what you are doing.

Upvotes: 1

Yuck
Yuck

Reputation: 50855

I'd use something like this:

var input = "abc";
var parsing = input.ToCharArray();
var result = new StringBuilder();
var offset = (int)'a';
foreach (var c in parsing) {
    var x = c - offset;
    result.Append(parsedText[x]);
}

Upvotes: 2

Chuck Norris
Chuck Norris

Reputation: 15190

Here is what you want. You can use Array.IndexOf(oldText, s) to get index of character in old array and then get value in new array by that index.

string input="abc";
char[] oldText = new char[26] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char[] newText = new char[26] { 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','a'};

char[] array = input.ToCharArray();
StringBuilder output= new StringBuilder();
foreach(char s in array)
{
  output.Append(newText[Array.IndexOf(oldText, s)]);
}

Console.WriteLine(output.ToString()); // "bcd"

Upvotes: 0

Related Questions