简体   繁体   中英

C++ Char Array deleting repeated characters

Hi im just looking for help with this, i wrote aa quick piece of code to take in a character array which will then be run through a function which will delete any repeated characters i have a small bug as its not deleting the last repeated letter here is the code i will also put in the output after the code..

#include <iostream>

#include <fstream>

#include <ostream>

using namespace std;

const int max_num_chars=10;

void deleteRepeats(char c[], int& size);


int main()
{
    char c[]={'a','b','b','b'};
    int c_size=4;

    cout<<"This Program reads characters into a partially filled array and then delete's repeats! \n";
    cout<<"Here is the original array \n";
    for (int i=0;i<c_size;i++)
    {
        cout<<c[i]<<"\n";
    }
    deleteRepeats(c, c_size);
    cout<<"Here is the array after the deleteRepeats function! \n";
    for (int i=0;i<c_size;i++)
    {
        cout<<c[i]<<"\n";
    }
    system("pause");
    return 0;
}
void deleteRepeats(char c[],int& size)
{
    int num = size;
    int start = 0;

    while(start != num)
    {
        char test = c[start];
        for(int i = start+1; i <= num;i++)
        {
            if(test==c[i])
            {
                for(int j = i;j<num;j++)
                    {
                    c[j] = c[j+1];
                    }
                num-=1;
            }
        }
    start +=1;
    }
    size = num;
}

Here is the output... This Program reads characters into a partially filled array and then delete's re peats! Here is the original array abbb Here is the array after the deleteRepeats function! abb Press any key to continue . . .

Sorry i just figured this out myself by adding this bit of code it fixed it

for(int j = i;j<num;j++)
                    {
                    c[j] = c[j+1];
                    }
                num-=1;
                start-=1; 

Though there is a better algorithm for this. In Your case :

Inside the function "deleteRepeats" , inside the loop "for(int i = start+1; i <= num;i++)"

when you are deleting the next same element, you are incresing the 'i' without considering that now at the same 'i' there can be duplicate element after the deletion also.

Solution : After you delete the element, decrement the value of i also. So now your loop looks like this

for(int i = start+1; i <= num;i++)
    {
        if(test==c[i])
        {
            for(int j = i;j<num;j++)
                {
                c[j] = c[j+1];
                }
            num-=1;
          // correction
            i--;
          // correction
        }
    }

If you have any problem understanding , please reply...

Why have two nested loops when you only need one?

void deleteRepeats(char c[], int& size) {
    // Trivial case of empty array: do nothing.
    if(size <= 0) // Why is `size` a signed `int` and not `size_t`?
        return;

    // First character is automatically not a repetition.
    // So for each character from the second onward,
    // move it if it is different from the previous character,
    // otherwise skip over it.
    char* read = c + 1;
    char* write = read;
    char* end = c + size;
    char prev = *c;
    while(read != end) {
        if(*read != prev) {
            prev = *read;
            *write++ = prev;
        }
        ++read;
    }

    // Finish up.
    size = write - c;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM