Write a c function called diff() with the prototype below that takes as input two null terminated char arrays (i.e. strings) a and b and returns the difference string consisting of all chars in a that are not in
b. the returned chars should be stored in a null terminated char array in the same order they appeared in a, possibly with repetitions.

Respuesta :

```
#include <stdlib.h>
#include <string.h>


char* diff( char a[], char b[] )
{
    int len, i, k, j = 0;
    char *c;

    len = ( strlen( a ) > strlen( b )) ? strlen( a ): strlen( b );
    if( ( c = calloc( len, 0 ) ) == NULL )
        exit( 1 );

    for( i = 0; i < len; i++ )
    {
        if( ( a[ i ] == 0 ) && ( b[ i ] == 0 ) )
        /* we're done */
            break;

        else if( ( a[ i ] == 0 ) && ( b[ i ] != 0 ) )
        /* b is longer, put chars into c */
        {
            k = i;
            while( b[ k ] != 0 )
            {
                c[ j ] = b[ k ];
                j++;
                k++;
            }
            break;
        }

        else if( ( a[ i ] != 0 ) && ( b[ i ] == 0 ) )
        /* a is longer, put chars into c */
        {
            k = i;
            while( a[ k ] != 0 )
            {
                c[ j ] = a[ k ];
                j++;
                k++;
            }
            break;
        }

        else
        {
            if( a[ i ] != b[ i ] )
            /* the chars don't match, put in c */
                c[ j ] = a[ i ];
                j++;
        }
    }
    return( c );
}

int main( int argc, char *argv[] )
{
    if( argc == 3 )
    {
        printf( "%s\n", diff( argv[ 1 ], argv[ 2 ]) );
        exit( 0 );
    }
    else
    {
        fprintf( stderr, "\nusage: %s <string> <string>\n\n", argv[ 0 ] );
        exit( 2 );
    }
}
```