Value does not return properly

I have put some debugging strings in the functions.
I cannot figure out why the last value does not return properly.

Points of interest is the comment box from line 9-22 and the calling function from line 77-81
Any suggestions?

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <iterator>
using namespace std;

/////////////////////////////////////////////////////////////////////////////////
int uniqueIndex(vector<int> indices, string str, vector<string> vec, int a)
{
	int it = find(vec.begin() + a,vec.end(),str) - vec.begin();
	
	if(find(indices.begin(),indices.end(),it) == indices.end())
	{
		cout<<"value returned   "<<it;  //testing
		return it;
	}

	uniqueIndex(indices,str,vec,a + it + 1);
       
        // return 0;
 /*   if i include this return 0, then 0 is received by the calling function. 
       why is the reutrn it; above not executed for last iteration.
*/
}
//////////////////////////////////////////////////////////////////////////////////

bool anagram(string a, string b)
{
	for(int i = 0; i < a.length(); i++)
	{
		if(!isalpha(a.at(i)))
		{
			a.erase(a.begin()+i);
			i--;
		}
	}
	
	for(int i = 0; i < b.length(); i++)
	{
		if(!isalpha(b.at(i)))
		{
			b.erase(b.begin()+i);
			i--;
		}
	}
	transform(a.begin(),a.end(),a.begin(),tolower);
	transform(b.begin(),b.end(),b.begin(),tolower);

	sort(a.begin(), a.end());
	sort(b.begin(), b.end());

	return (a == b);
}
void SameCharIDX(string in, vector<vector<int> > & out) 
{
	vector<string> vec;
	//now tokenize in.
	string xx; //each word
	stringstream ss;
	ss << in; //put in in ss

	while (getline(ss, xx, ','))
	vec.push_back(xx);

	vector<string> temp(vec);	//copy vec to temp.

	for (int i = 0; i < vec.size(); i++)
		cout << vec.at(i) << " , ";
	cout << endl;

	for (int i = 0; i < temp.size(); i = 0)	//look critically
	{
		string key = temp.at(i);
		vector<int> indices;	//indeces of anagrams

		for (int j = 0; j < temp.size(); j++)
		{
			if (anagram(key, temp.at(j)))	//inluding temp(0), coz a word is an anagram of iteself
			{
			////////////////////////////////////////////////////////////////
				int index = uniqueIndex(indices,temp.at(j),vec,0);
		//////															//////
				cout<<"  -  "<<index<<"  Value revieved"<<endl;
			/////////////////////////////////////////////////////////////////
				indices.push_back(index);
				temp.erase(j + temp.begin());
				--j;
			}
		}
		out.push_back(indices);
	}
	return ;
}
int main()
{
	string in = "Book, c++ is great, Boko, cigarets, time, koob, Mite, time";
	vector<vector<int>> out;
	SameCharIDX(in, out);

	cout << endl<<endl<<"Now printing the index of words with identical alphabetic chars\n";
	for (int i = 0; i < out.size(); i++)  //print vector
	{
		if(out[i].size() > 1)	//print only if there is more than one anagram.
		{
			for (int j = 0; j<out[i].size(); j++) //5
			{
				cout << out[i][j] << ", ";
			}
			cout << endl;
		}
	}
	system("pause");
	return 0;
}
Last edited on
line 30/39: Are you aware that you erase everything from the position to the end of the string? Read this:

http://www.cplusplus.com/reference/string/string/erase/

line 31/40: Why do you decrease i?

line 75: What is the purpose of passing a single char here?

Why do you pass two strings to anagram(...) at all? I mean you want to rearrange the same string, don't you?

What exactly is the purpose of your program?
Does really need to be a recursive method?
@coder777 :

line 30/39: Are you aware that you erase everything from the position to the end of the string?
line 31/40: Why do you decrease i?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
string str("abc++def");
for(int i = 0; i < str.length(); i++)
{
     if(!isalpha(str.at(i)))
     {
          // str.erase(i);   Yes erase everything
          str.erase(str.begin() + i); //erase just index i

        //Let i = 3, str.length() = 6, str = "abc+def";
        //in the next iteration, i = 4,  '+' is now in position 3.
        //without i--, '+' will be  MISSED
         i--;
     }
}

line 75: What is the purpose of passing a single char here?

Look at line 58,67,75.

Why do you pass two strings to anagram(...) at all? I mean you want to rearrange the same string, don't you?

Im checking if the 2 strings are anagrams of each other.

What exactly is the purpose of your program?

Print the indices of anagrams in a string.
Run the program without the test strings in line 16 and 85 and see.

@mutex
Does really need to be a recursive method?

It was easy to implement.

No ideas??

why is the reutrn it; above not executed for last iteration.


because it isn't found. line 20:

 
uniqueIndex(indices,str,vec,a + it + 1);

on it's own isn't doing anything at all. Do you not want something like this instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
       
        int rtn = 0;
        int it = find(vec.begin() + a,vec.end(),str) - vec.begin();

	

	if(find(indices.begin(),indices.end(),it) == indices.end())
	{
		cout<<"value returned   "<<it;  //testing
		rtn = it;
	}
        else
        {
                rtn = uniqueIndex(indices,str,vec,a + it + 1);
         }
	
       
        return rtn;

?

it's also really difficult to follow your code due to your nondescript variable names ('a', 'vec', 'str', 'ss' etc)

edit: recursive functions may be easy to implement, but sometimes they're a pain in the backside to debug.
Last edited on
Thanks mutex. Your function worked.

But getting back to mine,
why is the reutrn it; above not executed for last iteration.
because it isn't found. line 20:


yes. It is supposed to return if not found.

1
2
3
4
5
if(find(indices.begin(),indices.end(),it) == indices.end())
{
	cout<<"value returned   "<<it;  //testing
	return it;
}

But i dont still get why it didnt for the last iteration.

uniqueIndex(indices,str,vec,a + it + 1);on it's own isn't doing anything at all.

The body of the if executes if not found and returns.
If this does not occure, then the uniqueIndex(indices,str,vec,a + it + 1) will execute.
Topic archived. No new replies allowed.