recrusive function logic trouble :(

Hello, I was preparing myself for the upcoming semester when I came across this problem that has caused me a bit of trouble. This is a program i've written that will accept two words from the user and use an iterative test and a recursive test to determine whether or not the words are anagrams. It will also get rid of unnecessary space and/or non alpha/numerical characters. The only part of the program that I am stuck on is the recursive part. If I enter these two words, ( word 1: kayleb word 2: kayllb ) I get the message that they passed the recursive test and failed the iterative. they should fail both tests, can anyone see the problem, I'm 99% sure it lies in the isAnagram_recursive function.
Thanks for any amount of input you guys may have.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
 #include <iostream>
#include <string>
#include <cctype>

using namespace std;

class anagram
{
private:
	string input1, input2, mod1, mod2;
	string preprocess(string str);



public:

	anagram(string one, string two)	
	{
		mod1 = preprocess(one);
		mod2 = preprocess(two);
	}

	bool isAnagram_iterative();
	bool isAnagram_recursive(int);
	

};

bool anagram::isAnagram_iterative() 
{
	if (mod1.size() != mod2.size())
	{
		return false;
	}
	for (int i = 0; i<mod1.size(); i++)
	{
		if (mod1[i] != mod2[i])
		{
			return false;
		}
	}
	return true;

}

bool anagram::isAnagram_recursive(int i)
{
	
	if (mod1.size() != mod2.size())
	{
		return false;
	}
	 if (i = mod1.size() - 1)
	{
		if (mod1[i] == mod2[i])
		{
			return true;
		}
		else return false;
	}
	 if (i < mod1.size())
	{
		 if (mod1[i] != mod2[i])
		 {
			 return false;
		 }
		if (mod1[i] == mod2[i])
		{
			if (isAnagram_recursive(i++))
			{
				return true;
			}
			else  return false;
		}
		
	}
	return true;
}

string anagram::preprocess(string str)
{											
	int num_nonletters = 0;
	for (int i = 0; i <str.size(); i++)
	{
		if (str[i] == toupper(str[i]))
		{
			str[i] = tolower(str[i]);
		}

		if (ispunct(str[i]))
		{
			num_nonletters++;
		}
		if (isspace(str[i]))
		{
			num_nonletters++;
		}
	}

	char  temp;
	bool swap;

	do
	{
		swap = false;
		for (int count = 0; count < (str.size()-1); count++)
		{
			if (str[count] > str[count + 1])
			{
				temp = str[count];
				str[count] = str[count + 1];
				str[count + 1] = temp;
				swap = true;
			}
		}
	} while (swap);
	str = str.substr(num_nonletters);
	return str;
}

int main()
{

	string word1, word2;
	int count = 1;
	string ans;
	do{
	
		cout << "input: " << count << endl;
		cout << "Enter first word: \n";	
		getline(cin, word1);
		cout << "Enter second word: \n";
		getline(cin, word2);
		anagram object(word1, word2);	
		
		
		if (object.isAnagram_iterative())	
		{
			cout << "Iterative test passed!\n";
		}
		else
		{
			cout << "Iterative test FAILED!\n";
		}
		
		if (object.isAnagram_recursive(0))
		{
			cout << "recursive test passed!\n";
		}
		else
		{
			cout << "recursive test FAILED!\n";
		}
		cout << "run again y/n?\n";
		cin >> ans;
		cin.ignore(1000, '\n');
	} while (ans == "y");
}
		
Your program seems excessively complex. It would be much simpler to write an is_anagram() function that takes two strings (pass by value if you don't want to modify the strings), sorts the characters in the strings, then sees if the resulting sorted strings are the same. This may not answer your original question, but sometimes the answer is that *Yoda voice* asking the wrong question you are, mmmmhhhmmm.

Yes, it's late where I am...
Topic archived. No new replies allowed.