1

My project wants to look at the array and get out every two neighbors members together in just one element in the new array. I tried to do that in 2 different ways but all of them didn't work well, I can't find the reason.

public static void DviderWords() 
{
    string str = "Alameer Ahmed Amr Ali alameer .";
    string[] oops = str.Split(' ', ',', '!');
    int stringCounter = oops.Length;
    string[] holder = new string[10];
    for (int i = 0; i < stringCounter; i++)
    {
        for (int j = i + 1; j > i; i--)
        {
            string newVarible = oops[0+i] + oops[1+i];
            holder[i] = newVarible; 
        }
    }
}
3
  • can you please show the expected result Commented Nov 17, 2015 at 1:28
  • Can you provide the expected output of this method? Commented Nov 17, 2015 at 1:30
  • if you consider the coming line an array so it must be {AlameerAhmed , AhmedAmr , AmrAli , Alialameer} Commented Nov 17, 2015 at 1:31

1 Answer 1

2

I would do something like this. You only need the one for loop, not two.

public static void DviderWords() 
{
    string str = "Alameer Ahmed Amr Ali alameer .";
    string[] oops = str.Split(' ', ',', '!');
    int stringCounter = oops.Length;
    string[] holder = new string[stringCounter - 1];
    for (int i = 0; i < stringCounter - 1; i++)
    {
        holder[i] = oops[i] + oops[i + 1];
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

it's not working well i have tried this i guess it because the loop pointer can't hold 2 elements together simultaneously !
Are you aware of the '.' at the end? When I ran the code, I got the following array ["AlameerAhmed", "AhmedAmr", "AmrAli", "Alialameer", "alameer."]. What is incorrect with this?
thanks pro. very much the error when i print that out , i focus on the old array so i expect that the code is wrong . thanks again !
Please mark the answer as the answer if it is correct.

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.