Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello ..
Am Facing a problem with comparing 2 arrays values ..

my output that i need is :
if the text = " hi"
then the new array will be like this :
note where the change is :- first 2 elements ...
{"h","i","a","b","c","d","e","f","g","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};

my code do every thing right but my problem is when i want to shift array elements from the first to where the (text from textbox ) ends ..

the code am using is :
C#
static String[] arrayofstring = {"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"};
String[] arraywithkey = new string[arrayofstring.Length];

private void btn_Click(object sender, EventArgs e)
        {

            // reading textbox and save it's value into new array with name arraywithkey 
            int txtlength = txttext.Text.Length - 1;
            for (int i = 0; i <= txtlength; i++)
            {
                    string s = txttext.Text.Substring(i, 1);
                    arraywithkey[i] = s;
            }

                // will check all values of the array
                for (int j = 0; j <= 25; j++)
                {
                    // check values of the new array and add new values to it 
                    for (int h = txtlength+1 ; h <= 25; h++)
                    {
                        // compare 2 arrays if value found skip else add value to new array
                        if (arraywithkey[j] == arrayofstring[j])
                        {
                            break;
                        }
                        else
                        {
                            MessageBox.Show("h is :"+h);
                            MessageBox.Show("j is :"+j);
                            arraywithkey[h] = arrayofstring[j];
                        }
                    }
                }
        }


thanks in advance ...
Posted
Updated 23-Mar-13 6:52am
v2
Comments
Zoltán Zörgő 23-Mar-13 13:42pm    
Please give a more comprehensive specification of your function. The example you gave is not enough.
waleedquwaider 23-Mar-13 13:51pm    
In General :
i have 2 arrays .. first array filled with strings and it's value is alphabet ..
second array will contain a word that you entered ...
now we are going to merge these array's and check if the letter in the second array found in first array we are not going to add it else we are going to add the left of letter's .. and so
Zoltán Zörgő 23-Mar-13 14:10pm    
Ok. First of all: why array of strings? You waste lot of resources, array of char would be enough. But if every element in the array is a single character string, you can do all this with strings, without any array...

1 solution

Hi,

If I am not wrong, actually you need to swap array according to data provided in textbox.
Such that data in text box will be first in array.
You can try like below given.

C#
String[] arrayofstring = { "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" };
            String[] arraywithkey = new string[arrayofstring.Length];

            string text = txttext.Text;       // "hi";

            String[] tmpArrary = new String[text.Length];

            for (int i = 0; i < text.Length; i++)
            {
                string s = text.Substring(i, 1);
                tmpArrary[i] = s;
            }

            arraywithkey = tmpArrary.Union(from s in arrayofstring where !tmpArrary.Contains(s) select s).ToArray();
 
Share this answer
 
Comments
waleedquwaider 6-Oct-13 5:20am    
Thanks alot that really helped :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900