Need to loop once more.

So this program should delete copies in an array.
for example: user inputs 'KAADD' the output should be 'K'.
Also if the user inputs 'ABBA" the output should be nothing since
the 'BB' is removed resulting is 'AA', but that also is a duplicate so it gets erased as well.

My problem is my program only deletes a pair once and doesnt check for any other pairs. Any help?

I need to state that i can only use arrays, not vectors. If it can't be done with arrays, pls let me know.

#include <iostream>
#include <string>

using namespace std;

string deduplicate(string input)
{
for(int i = 0; i <= input.length(); i++)
{
if (input[i] == input[i+1])
{
input[i] = input[i+2];
input[i+1] = input[i+3];
input[i+2] = '\0';
input[i+3] = '\0';

}

}


return input;
}


int main()
{
string input;
cout << "Enter a string: ";
cin >> input;
cout << deduplicate(input);
cout << endl;


return 0;
}
Last edited on
If the input is 'ABBA', should you remove everything (for both A and B do repeat) or just the BB -- a consecutive repeat?
What if a character repeats more than two times?


This site has reference documentation for the standard library. The standard library has algorithm named std::remove. Take a peek on its description.
if input is 'ABBA' it should remove everything. Only problem is to get that, I feel like I need to put the two A's together once the B's are removed.

if input is 'AAA' only one pair of consecutive characters are removed. Output = A
if 'AAAA', output = "Empty"
Last edited on
for each character
  find next copy of that character
  if there is a duplicate
  then remove the two characters

'ABBA'
1. looking at first A, there is another A
=> remove both
2. looking at first B, there is another B
=> remove both
3. at end
string is empty

'AAA'
1. looking at first A, there is another A
=> remove both
2. looking at first A, there is no duplicate
3. at end
string has 'A'

'ABAAKA'
1. looking at first A, there is another A
=> remove both
'BAKA__'
2. looking at first B, there is no duplicate
3. looking at first A, there is a duplicate
=> remove both
'BK____'
4. looking at first K, there is no duplicate
5. at end
string has 'BK'

Do you notice that you have to move (copy) all remaining array elements left in order to remove something?

Note that only the last occurrence of each character remains (or none).

I need to state that i can only use arrays, not vectors.

You already have a std::string. A string is not a vector nor an array.
Last edited on
Your code should look like this:
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
#include <iostream>
#include <string>

using namespace std;

string deduplicate(string input)
{
	int j = 0;
	while(input.length() / 2 > j)
	{
		for(int i = 0; i < input.length(); i++)
			{
				if (input[i] == input[i+1])
				{
					input[i] = input[i+2];
					input[i+1] = input[i+3];
					input[i+2] = '\0';
					input[i+3] = '\0';
				}
			}
		j++;
	}
return input;
}


int main()
{
string input;
cout << "Enter a string: ";
cin >> input;
cout << deduplicate(input) << endl;

return 0;
} 
Your code should look like this:
Most likely not.

Have you heard of out of range error?

The first time you run the loop on line 11, on its last iteration, the i+1==input.length()
On line 13 you dereference input[i+1], which is thus input[input.length()].

Lets pretend that it does not crash the program and condition is true.
You would reach line 18: input[input.length()+2] = '\0';

Just kidding, that code attempts to write past array (out of range) already on earlier iterations. Crash and burn.


The same goes for OP code. I probably should have pointed that out earlier.


The use of code tags ... how code looks when posted ... yes, yes, please use them.
Topic archived. No new replies allowed.