Click here to Skip to main content
15,897,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been tasked with re-writing some C++ Code into .NET. Unfortunately my C++ skills are very weak. Could someone with far better C++ Skills than I explain what the following method does with maybe a sample. Thanks in advance.

C++
int pivSubCompare(char *strInput1, int intSP1, int intEP1, char *strInput2, int intSP2, int intEP2)
{
	int   intLoop1, intLoop2;
	int   intMax = 0;
	int   intCnt, intNS1, intNS2;
	char *p1, *p2;

	intNS1 = intNS2 = -1;
	intCnt = 0;

	if((intSP1 > intEP1) || (intSP2 > intEP2) || (intSP1 < 0) || (intSP2 < 0))
		return 0;

	for(intLoop1 = intSP1; intLoop1 < intEP1; intLoop1++)
	{
		for(intLoop2 = intSP2; intLoop2 < intEP2; intLoop2++)
		{
			p1 = strInput1 + intLoop1;
			p2 = strInput2 + intLoop2;
			intCnt = 0;

			while((*p1 == *p2) && (*p1 != '\0') && (*p2 != '\0'))
			{
				intCnt++;

				if(intCnt > intMax)
				{
					intNS1 = intLoop1;
					intNS2 = intLoop2;
					intMax = intCnt;

					if(intMax == (intEP2 - intSP2)) return intMax;
				}

				p1++;
				p2++;
			}
		}
	}

	intMax += pivSubCompare(strInput1, intNS1 + intMax, intEP1, strInput2, intNS2 + intMax, intEP2);
	intMax += pivSubCompare(strInput1, intSP1, intNS1 - 1, strInput2, intSP2, intNS2 - 1);

	return intMax;
}
Posted
Comments
Sergey Alexandrovich Kryukov 21-Oct-11 12:08pm    
Looks easy, but -- do you really need to translate it? It could be much faster to just write it in C# if you specified what the function should do. Can you do that instead?
--SA
Sergey Chepurin 21-Oct-11 14:41pm    
String comparison using raw pointers in pure C. Would be very inefficient way to reproduce in C#.

1 solution

Here's a suggestion for self discovery... you have the code, why not put it into a simple application then walk through it to see what it's doing (any debugger will allow you to step through)?

The function prototype indicates that it takes two input strings and starting/ending points for each and does some sort of comparison (character by character comparison). So make a simple application that passes two different strings and see what it's doing, then you can take SA's advice and instead of trying to replicate what it's doing in C++ line by line, you can simply write your own version in .NET (whatever .NET language you're using).
 
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