Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created May 2, 2024 14:47
Show Gist options
  • Save bjoerntx/2e98b94cdb158cc09599f15896dbfa7e to your computer and use it in GitHub Desktop.
Save bjoerntx/2e98b94cdb158cc09599f15896dbfa7e to your computer and use it in GitHub Desktop.
private static List<(string word, int charIndex, string replacedWord)> CompareSentences(string sentence1, string sentence2)
{
string[] words1 = sentence1.Split(' ');
string[] words2 = sentence2.Split(' ');
List<(string word, int charIndex, string replacedWord)> differences =
new List<(string word, int charIndex, string replacedWord)>();
// Track the character index
int charIndex = 0;
// Get the maximum length of the two sentences
int maxLength = Math.Max(words1.Length, words2.Length);
// Compare each word in the sentences
for (int i = 0; i < maxLength; i++)
{
// Check if the current word exists in both sentences
if (i < words1.Length && i < words2.Length)
{
// If the words are different, add the word, character index, and replaced word to the list
if (words1[i] != words2[i])
{
differences.Add((words1[i], charIndex, words2[i]));
}
}
// If one of the sentences is shorter, add the extra word to the list
else if (i < words1.Length)
{
differences.Add((words1[i], charIndex, ""));
}
else
{
differences.Add((words2[i], charIndex, ""));
}
// Update the character index for the next word
if (i < words1.Length)
charIndex += words1[i].Length + 1; // Add 1 for the space
}
return differences;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment