Ofek Pintok
Ofek Pintok

Reputation: 623

removing duplicated chars from string in C

My program has to look for duplicated chars (that comes one after another) and remove them. So my program does work, but in case I put "PLLNSIS", I get "PLNSISS". I overwrite the char that I find out is duplicated, but in the end, i recieve a copy of the last char.

 void main() 
    {
        int length;
        char *myString;
        printf("Enter the length of the string \n");
        scanf("%d", &length);
        myString = (char*)malloc((length+1) * sizeof(char));
        assert(myString);
        printf("Now enter the string: (max %d letters) \n", length);
        fseek(stdin, 0, SEEK_END); //flushing the buffer
        fgets(myString, length+1, stdin); //calling the input function
        no_Straight_Letters(myString);
        free(myString);
    }     

       void no_Straight_Letters(char *myString)
        {
            int i, j = 0, length = strlen(myString);
            for (i = 1; i < length-1; i++)
            {
                if (myString[j] == myString[i])
                {
                    myString[j] = myString[i];
                    myString[i] = '\0';
                }
                    else myString[++j] = myString[i];
            }
            myString[length] = '\0'; //last char of the string
            printf("And the new string is.... -->  ");
            puts(myString);
        }

I found out the cause but when I fix it, I get nothing on the screen.

Upvotes: 0

Views: 83

Answers (2)

user2736738
user2736738

Reputation: 30926

The problem is

 myString[length] = '\0';

It will be

 myString[++j] = '\0';

This works because at the end of the looping j points to the last valid character. Then if you increase it and put \0 there - it will make the string.

You can emulate the behavior of having \n with this small addition (which is not likely to be needed).

myString[++j]='\n';
myString[++j]=0;

Also as a small modification you can remove the redundant assignment in your code. it's unnecessary.

if (myString[j] == myString[i])
{
    myString[i] = '\0';
}

Upvotes: 2

Stephan Lechner
Stephan Lechner

Reputation: 35154

There are two issues:

First, you miss the last character; you might not have noticed it, because the last character of your fgets-input is probably a newline which you do not see. So you'd iterate like for (i = 1; i <= length-1; i++).

Second, you cut off the string at length, which is larger then the final string; use myString[j+1] = '\0' instead.

Minor issue: The code in your if is useless, you can omit it.

void no_Straight_Letters(char *myString)
{
    int i, j = 0, length = strlen(myString);
    for (i = 1; i <= length-1; i++)
    {
        if (myString[j] != myString[i])
           myString[++j] = myString[i];
    }
    myString[j+1] = '\0'; //last char of the string
    printf("And the new string is.... -->  '%s'",myString);
}


int main()
{
    char myString[] = "PLLNSISSSHERBERTTTA";
    no_Straight_Letters(myString);
}

Upvotes: 2

Related Questions