This is my code
private double wordcompare(word a, word b)
{
if (a.AtTheEndOf(b))
return -1;
double matchPer=word.Compare( a,b);
Console.WriteLine(matchPer);
a.shiftToleft();
double nextMatchPer = wordcompare(a, b);
return matchPer > nextMatchPer ? matchPer : nextMatchPer;
}
the Compare() method compare 2 words and return a percentage of match letters the shifttoleft() method move the compared word one step virtually, I have been debugged every step when it arrives to the return of Compare() there is numbers like 2/3 or 1/3 but when but when i print the returned value it shows 0 except the first call help please.
word class
class word
{
public static double Pertolarnce = 0.2;
private int pos;
public int wordTolarnce;
string letters;
public int Pos { get{ return pos; } }
public word(string s)
{
letters = s;
wordTolarnce=(int)Math.Round(Pertolarnce * s.Length, MidpointRounding.AwayFromZero) ;
pos = 0 - wordTolarnce;
}
public void shiftToleft()
{
pos++;
}
public int Len { get { return letters.Length; } }
public static double Compare(word a,word w)
{
int comLeters = 0;
int matchedLeters = 0;
int i, j;
i = 0 - a.pos;
j = 0 + a.pos;
i=i < 0 ? 0 : i;
j=j < 0 ? 0 : j;
while(i<a.Len && j<w.Len )
{
comLeters++;
if (a.letters[i] == w.leterAt(j))
matchedLeters++;
i++;
j++;
}
return matchedLeters / comLeters;
}
private char leterAt(int index)
{
return letters[index];
}
public bool AtTheEndOf(word w)
{
if(w.Len-this.pos+1 == this.Len-wordTolarnce)
return true;
else
return false;
}
}
output of
Console.WriteLine(matchPer);
1
0
0
0
0
0
shiftToLeftdoes, orwordcompare, or how you're calling the method, or what result you expect, or what result you get. I'd also very strongly advise you to start following .NET naming conventions.wordcomparemethod appears to change theaobject, this is really not a good idea. A comparison method should ideally be pure, ie. it takes input, it returns a result based on that input, and doesn't modify anything.