I am having troubles with my deleting_duplicates function at the end and cannot return the correct values/letters. The project is to delete any repeating letters from the initial phrase we are given and then display the new phrase without duplicate letters. Any help would be much appreciated!
P.S. I am a beginner so their will probably be obvious mistakes..
Eeeeeh, no, not quite. The loops all seem okay, but I believe you were supposed to go down the loop copying values one over to the left, which means that line 49 isn't right. What it's doing is copying whatever is at index j to the rest of the array, which is making your duplication issue worse.
Line 50 should not be in the innermost while loop.
I'm not sure what purpose you're intending line 51 to serve. O.o
I completely got rid of line 51 and changed array[k] to array [k-1]? You are correct when saying the loop is suppose to copy values over to the left, but I am not sure what exactly to include in the body of the delete function. Any pointers/tips/examples will help out tremendously.
Here is the algorithm I'm working with. I switch line 50 to inside the last for loop
For each character in the array
For each character beyond the one the outer loop is on
if the character ffrom the inner loop matches the one from the o outer loop
For all characters beyond the current one
copy the character to the prior array element
decrement size
1. for each character in the array
a. for each character beyond the one the outer loop is on
i. if the character from the inner loop matches the one from the outer loop
1. for all characters beyond the current one
a. copy the character to the prior array element
2. decrement size
1) loop from the first letter in the string until the second to last (*valid) letter.
2)If the current letter is the same as the next letter shift all the letters from that position ( i + 1 ) to the last letter left by one. ( loop from the letter after the current till the second to last )
here is a revised version of the one I posted earlier:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int i = 0;
int duplicates = 0;
while ( i < size - duplicates - 1 )//you should use a while loop since the size will be changing
{
if( array[i] == array[i+1] )
{
for( int j = i + 1; j < size - duplicates - 1; ++j )
{
array[j] = array[j+1];
}
++duplicates;
}
++i;
}
size -= duplicates;
*edit yes you are correct booradley I didn't think about more than two characters. Sorry took so long typing lol.
You're running off the end of an array somewhere (i.e. using an index value that exceeds the bounds of the array). Do you have a debugger at your disposal?
I see that k (declared on line 37) is never initialized to anything. That's a problem, too.