Patel
Patel

Reputation: 87

Repeated character in a string

I am writing a code to remove the repeated occurrence of a character in the string.

Description:- Remove the repeated characters from the string

Example:-

Sample Input = abcdeabd

Sample Output =abcde

I have written the code and it is working and when I tested by running sample test cases ,It is passing most of the test cases but is failing some e.g. when I Use the input string as "abcdabcdabcdabcd" It is giving me abcdd as the output instead of "abcd"

Here is my code

#include<stdio.h>

int main(void)
{

        char a[60]="abcdeabd";
        int n=0;
        for(int l=0;a[l]!='\0';++l)
            ++n;
        printf("%d\n",--n);
        for(int i=0;i<=n;++i)
        {
            for(int j=i+1;j<=n;++j)
            {
                if(a[i]==a[j])
                {
                    for(int k=j;k<=n;++k)
                        a[k]=a[k+1];
                    --n;

                 }    
            }    
        }
      puts(a); 
  return 0;      
}

Please tell me where I am going wrong with this code...?

Upvotes: 0

Views: 130

Answers (4)

user388229
user388229

Reputation: 109

@Patel: Your program is correct. You missed only one single thing. When You used printf(), you decrement the value of n.

So put a line to increment it after printf()

++i;

Upvotes: 0

user388229
user388229

Reputation: 109

#include<stdio.h> 
int main(void) 
{ 
 char a[10]="abcdeabd"; 
 for(int i=0;a[i]!='\0';++i)
                printf("\n %c", a[i]);

 for(i=0;a[i]!='\0';++i)
    for(int j=i+1;a[j]!='\0';++j)
        if(a[i]==a[j])
            for(int k=j;a[k]!='\0';++k) 
                  a[k]=a[k+1];
 puts(a); 
 return 0;
}

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 311048

As for me I would write a corresponding function using pointers. For example

#include <stdio.h>

char * unique( char *s )
{
    char *last = s, *current = s;

    do
    {
        char *t = s;
        while ( t != last && *t != *current ) ++t;
        if ( t == last )
        {
            if ( last != current ) *last = *current;
            ++last;
        }
    } while ( *current++ );

    return s;
}

int main(void) 
{
    char s[]="abcdeabd";

    puts( s );
    puts( unique( s ) );

    return 0;
}

The output is

abcdeabd
abcde

As for your code then I would rewrite it the following way Take into account that you have to copy also the terminating zero.

#include <stdio.h>

char *unique( char *s )
{
    int n = 0;

    while ( s[n++] != '\0' );

    printf( "%d\n", n );

    for ( int i = 0; i < n; ++i )
    {
        for ( int j = i + 1; j < n; ++j )
        {
            if ( s[i] == s[j] )
            {
                --n;
                for ( int k = j; k  < n; ++k ) s[k] = s[k+1];
            }    
         }    
    }

    return s;
}


int main(void) 
{
    char s[]="abcdeabd";

    puts( s );
    puts( unique( s ) );

    return 0;
}

Upvotes: 1

R Sahu
R Sahu

Reputation: 206667

The logic error is in the block

 if(a[i]==a[j])
 {
    for(int k=j;k<=n;++k)
       a[k]=a[k+1];
    --n;
 }    

It doesn't work when you have the same character more than twice in succession. It doesn't work for `"addd" or "adddbc".

Change that to a while loop to fix the problem.

 while (a[i] == a[j])
 {
    for(int k=j;k<=n;++k)
       a[k]=a[k+1];
    --n;
 }    

Upvotes: 3

Related Questions