attempting to eliminate duplicate strings in an array

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
/*Function 5: Unique word array
Parameters: (string to search, integer start position, array to return words*/
int string_unique140(string str,int start,string words[],string unique[]){
	int length;
	int i = 0;
	bool unique_word = true;
	
	//loop to fill array with non-blank substrings
	while((start < str.length()) && (start != -1)){
			start = nxt_word140(str,start);
			if(start < 0) break;
			length = len_word140(str,start);
			words[i] = str.substr(start,length);
			i++;
			start = start + length;
	}
	sort_words140(words,i);

	for(int j=0;j<i;j++) unique[j] = "eggsaladsandwich"; //fill valid spaces of unique array with sentinal: "eggsaladsandwich"
	for(int j=0;j<i;j++){
		unique_word = true;
		while(unique_word){
			for(int k=0;k<i;k++){
				if((words[j] != unique[k]) && (unique_word)){
					unique[k] = words[j];
					unique_word = false;
				}
				else unique_word = false;
			}
		}
		
	}

	return i;
}


First post so apologies if I butchered it. I am currently trying to compare each element in my first string array (words[]) with each element in my output array (unique[]) and add each unique word to the latter. I am having trouble with my super-nested loops where I do my comparison and assignments. Any insight would be greatly appreciated.

Thanks,
Nate
Last edited on
Edit: removed profane cout statement that was relaying current variable information.. oops

To save posts, I also just realized that the way it is now, it is working to find any words w/o duplicity, but I need to record each word only once. I should have also supplied the array after the sort in the upper part of the function.

string words[] is valid as follows:
0[word1]
1[word1]
2[word2]
3[word2]
4[word3]
Last edited on
Topic archived. No new replies allowed.