I have a string of words:
spring java spring spring spring javescript java jboss jboss tomcat jboss
I have to break up this string using a space as the delimiter, then i two types of pairs I need to find
I know how to break up the words
string text = "spring java spring spring spring javescript java jboss jboss tomcat jboss";
string[] list = text.Split(' ');
and it works fine, but I don't know where to start on finding the pairs, the output should be like this:
spring: 6 Combo-pairs(0,2)(0,3)(0,4)(2,4)(3,4) and 2 next-to-pairs (2,3)(3,4)
so in short it must print out the word and then the position of all the other words identical to in relation to it, then continue to the next word and print the position of the rest in relation to the new word, I tried adding each word to a Dictionary and their index to another dictionary and compare them from the original array and get the index but I get so confused and I have no idea what to do or where to start, any advice or maybe a direction to look into ? I don't want the answer I just want a guideline if possible ?
This should do it ...
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
Define a container for each result item ...
class IndexPair
{
public int Index1 { get; set; }
public int Index2 { get; set; }
}
Ok lets do this ...
static void Main(string[] args)
{
Declare the string we want to test against and some result sets to store the results ...
var testString = "spring java spring spring spring javescript java jboss jboss tomcat jboss";
var test = testString.Split(' ');
var pairs = new List<IndexPair>();
var comboPairs = new List<IndexPair>();
Loop through the source array (variable "test") and ask the appropriate questions ...
for (int i = 0; i < test.Length; i++)
for (int j = 0; j < test.Length; j++)
if (j > i && test[i] == test[j])
{
var pair = new IndexPair { Index1 = i, Index2 = j };
pairs.Add(pair);
if (j == (i + 1)) comboPairs.Add(pair);
}
Output the results ...
Console.WriteLine("Input string: " + testString);
Console.WriteLine("Word pairs: " + string.Join(" ", pairs.Select(p => $"{p.Index1},{p.Index2}")));
Console.WriteLine("Combo pairs: " + string.Join(" ", comboPairs.Select(p => $"{p.Index1},{p.Index2}")));
Console.ReadKey();
}
}
}
... seems to give the expected output at least (even though the question didn't quite pick up on all matches).
Hopefully if you followed that right you should have realised that all the code from this answer combined is a full console app that will achieve what you want when run.