Looking at your examples, it seems that there are two types of comparisons taking place between pairs of strings.
- If the strings are of the same length, the characters at the same index that do not match each other are being selected.
- If the strings are of different lengths, the larger string’s characters are transversed and compared sequentially with characters in the smaller string. If there is a mismatch, the missing character is selected. When this case arises, the larger string’s index position is increased but the smaller string’s index is not. The transversal continues until the end of the larger string is reached. A sign character is attached to the missing character to indicate what string is the smaller, a ‘-’ for the second string and a ‘+’ for the first.
A good approach would be to use a value tuple to store the strings with the left string holding the larger string and a bool to indicate the position sign.
(string left, string right, bool isNegative)
. Tuples are useful as they are easy to deconstruct, lightweight and reduce the need to have to keep indexing into arrays. I would pad the shorter string with trailing spaces to avoid the ‘index out of bounds’ nonsense. The basic set up would be something along these lines.
for (int i = 0; i < TestData.Count - 1; i += 2)
{
(string left, string right, bool isNegative) = (TestData[i + 1].Length > TestData[i].Length) ?
(TestData[i + 1], TestData[i], false) : (TestData[i], TestData[i + 1], true);
bool isEqualLength = left.Length == right.Length;
right = right.PadRight(left.Length);
int p = 0;
for (int n = 0; n < left.Length; n++)
{
I hope this is of some help to you. Best wishes.