Diff Function

May 2, 2010 at 5:25pm
I'm writing yet another function that would determine how many characters in the first string must be changed to change the first string into the second. I'll provide a short example...
string1 = GHRIRMMRIEOFBB
string2 = GHRIKKRLIEAFBBTOOLS

The return value here would be 10.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int difference (const Protein Datalist[]) {
	int numLoops = 0;
	char ch1, ch2;
	string s1, s2, ss;
	int diff = 0, size1, size2;

	s1 = Datalist[0].Sequence;
	s2 = Datalist[2].Sequence;

	size1 = s1.length();
	size2 = s2.length();

	if (s1 < s2)
		ss = s2;

	if (s1 > s2)
		ss = s1;

	for (numLoops = 0; numLoops < ss.length(); numLoops++) {

		ch1 = s1.at(numLoops);
		ch2 = s2.at(numLoops);

		if (ch1 != ch2)
			diff++;

		numLoops++;
	}


	return (diff);
}

I believe I'm almost there but for some reason this code doesn't give me the right number. SS takes the larger of the 2 strings and loops it that many times. Any suggestions? Thanks for the help.
May 2, 2010 at 5:38pm
I'd recommend resizing the smaller string to the size of the larger string.
http://cplusplus.com/reference/string/string/resize/

Other than that, your code is fundamentally right, except that the content @Line 27 shouldn't be there.

-Albatross
May 2, 2010 at 5:50pm
Good call on the extra numLoops but I added "s2.resize(size1)" under the 2nd if statement and "s1.resize(size2)" under the 1st if statement and I'm not getting the right number still. I think it's getting everything correct except for the extra characters that s2 has.
May 2, 2010 at 5:53pm
Try adding some extra trash characters that nobody would use to the shorter string? Same function, you just need another argument.

-Albatross
May 2, 2010 at 10:40pm
for some reason when I use resize it's telling me "2 overloads have no legal conversion for 'this' pointer". resize worked fine when I had Datalist as my parameter but I have since changed passed the 2 strings needed directly to the function and now resize doesn't work.
May 2, 2010 at 11:05pm
Try just looping over the smaller string's # of chars, then after you are done with the loop just add in the difference in their sizes.
Topic archived. No new replies allowed.