2

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

6
  • you have two arrays called normalText ? Commented Apr 16, 2012 at 12:17
  • 1
    Do you want to check if they are equal or do you want to see if one is a subset of the other? Commented Apr 16, 2012 at 12:18
  • Why do you declare two normalText arrays, how are they related to each other? What's the rule to turn the input of "abc" into "bcd"? I'm afraid your question is currently not very clear. Commented Apr 16, 2012 at 12:18
  • 4
    a job for Dictionary? msdn.microsoft.com/en-us/library/xfhwa508.aspx Commented Apr 16, 2012 at 12:20
  • @TimSchmelter It looks like he wants to use the arrays to define a mapping by index. ie : arr1[1] = 'a' therefore return arr2[1] (= 'b') Commented Apr 16, 2012 at 12:22

4 Answers 4

2

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]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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"

Comments

0

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;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.