Click here to Skip to main content
15,881,665 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
c#: How do I check if index's of input string[] are next to each other ?


string[] a = {"one","two","three","four","five","six","seven","eight"};

string[] input = {"three", "one", "four", "two"};

if(input index's are next to each other)
{
MessageBox.Show("OK");
}
Posted
Comments
Sergey Alexandrovich Kryukov 25-Dec-15 15:50pm    
First, you need to define precisely and formally what this "next to each other" means. An example is not definition.
What have you tried so far?
—SA
Member 11380736 25-Dec-15 15:53pm    
index[0],index[2],index[1],index[3] == next to each other.
index[0],index[2],index[1],index[4] == ' NOT'next to each other.
Andreas Gieriet 25-Dec-15 17:08pm    
Still not clear.
What is "index[0]", etc.?
What are the allowed elements? "zero"? "minus three"? "one thousand three hundred seventy five"? How to decide if two elements are "next to each other"? In the sense of numeric values?
Andi
BillWoodruff 26-Dec-15 1:54am    
index[0],index[2],index[1],index[3] == next to each other.

does not make sense

1 solution

Get the indexes into an array.
Sort the array.
If the values in the array are all a single increment from the previous value, the strings are all next to each other.

There is an Array.Sort method in .NET, and a simple for or foreach loop will check the results.
 
Share this answer
 

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