Click here to Skip to main content
16,004,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently trying to convert the following strings into the following formats, but I am hitting a snag. I will send code to the closest thing that works, but it completely misses all but the last character in a change, and it also misses the extra letters that can be at the end of the string.

Here is an example of what I am looking for:

"Apple" and "Epple" should return "[A->E]"
"Epple" and "Apple" should return "[E->A]"
"Apple" and "Aple" should return "[-p]"
"Aple" and "Apple" should return "[+p]"
"Apples" and "Apple" should return "[-s]"
"Apple" and "Apples" should return "[+s]"
"The Apple" and "Apple" should return "[-T] [-h] [-e] [- ]"
"Apple" and "The Apple" should return "[+T] [+h] [+e] [+ ]"
- bonus points if you can "[+The ]" and "[-The ]"

What I have tried:

Here is the code I have so far:
C#
String previous = "Apple";
String current = "The Apple";

List<String> delta = new List<string>();

int i = 0;
int ii = 0;
List<char> section = new List<char>();

for (i = 0; i < previous.Length; i++)
{
    if (ii < current.Length - 1 && previous[i] != current[ii])
    {
        if (i < previous.Length - 1 && previous[i + 1] == current[ii])
        {
            delta.Add("[-" + previous[i] + "]");
            loggedDeltas.Add(false);
            lettersum -= previous.ToUpper()[i] - 64;
            binarysum--;
            //i++;
            ii--;
        }
        else if (ii < current.Length - 1 && previous[i] == current[ii + 1])
        {
            delta.Add("[+" + current[ii] + "]");
            loggedDeltas.Add(true);
            lettersum += current.ToUpper()[ii] - 64;
            binarysum++;
            ii++;
        }

        else if (i < previous.Length - 1 && ii < current.Length - 1 && previous[i + 1] == current[ii + 1])
        {
            delta.Add("[" + previous[i] + ">" + current[ii] + "]");
            lettersum += (current.ToUpper()[ii] - 64) - (previous.ToUpper()[i] - 64);
            if (current.ToUpper()[ii] - previous.ToUpper()[i] > 0)
            {
                loggedDeltas.Add(true);
                binarysum++;
            }
            else
            {
                loggedDeltas.Add(false);
                binarysum--;
            }
        }
    }
    ii++;
}
Posted

 
Share this answer
 
Comments
Daisy Eseyad 17-Jun-24 13:14pm    
Thanks, i'll see if I can understand which parts I need for a single line.
There are a lot of things that you can do to help yourself here, that will teach you to be a better developer. One of the primary techniques that you need to learn is how to debug code for yourself plus, I see you doing things in there that aren't being asked for in the question (there's no need to do case-sensitive comparisons).

Start small, and put in two character words such as Io and So. Step through your code, line by line and pay attention to the value of the index that you are comparing; this will tell you if you are checking what you think you are checking.

As a professional developer, you will spend a lot of time debugging code. This is your opportunity to start mastering a skill that will see you through the rest of your career. Good luck.
 
Share this answer
 
Comments
Daisy Eseyad 17-Jun-24 12:51pm    
Yes, I have been debugging my code, even trying to do a while statement, but the algorithm of detecting the differences escapes me. Every thing I have tried just... doesn't work.
Pete O'Hanlon 18-Jun-24 3:44am    
Break it down and try to solve one problem at a time. Concentrate on the first case, where a word changes by a single character. Try the example there first, and when you have fixed that, try another word; where a different letter changes (e.g. LEGO to LOGO). Take out the code that you think doesn't have any relevance to this, which will make your life easier. As you step over the code, look at the values of the variables to see if they match what you would expect them to.

Looking at your examples, it seems that there are two types of comparisons taking place between pairs of strings.


  1. If the strings are of the same length, the characters at the same index that do not match each other are being selected.

  2. 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.


C#
//step through the list in pairs
for (int i = 0; i < TestData.Count - 1; i += 2)
  {
    //use a Tuple to hold the pairs.
    //Set the isNegative flag if the right length is not greater than the left length
    // put the largest string on the left
    (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);
    //set an isEqualLength flag as equal length strings are treated differently
    bool isEqualLength = left.Length == right.Length;
    //pad out the right string if needed so both are of equal  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.

 
Share this answer
 
v2
What you're looking for is String metric algorithms[^]
There is few of algorithms, such as: Levenshtein distance - Wikipedia[^], Hamming distance - Wikipedia[^], etc.

Good luck!
 
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