Thread: c program for sort and remove duplicate values from an array

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    c program for sort and remove duplicate values from an array

    Hi ,

    I have a string array having a bunch of values. I need to sort this array, remove duplicate values from this array and reset the filtered array (without duplicate values) to a new array.

    I am new to C programming. I have written some code, but it fails with a core dump always. Can some one please correct my code or send me an easy way to do this.

    Thanks.
    Code:
    code snippet:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    main ()
    {
    int stat = 0;
    int ctr = 4;
    int inx = 0;
    int status = 0;
    int j = 0;
    int k = 0;
    int reset_count = 0;
    char **lov_reset_values = 0;
    char *str[] = { "test", "test1", "test2", "test" };
    
    for ( inx = 0; inx < ctr; inx ++ )
    {
    printf ( "str[%d] = %s\n", inx, str[inx] );
    }
    
    for(j=0;j<ctr;j++)
    {
    status = 0;
    for(k=1;k<=ctr;k++)
    {
    printf ( "entered after k= 0\n" );
    if( strcmp (str[j],str[k] ) == 0 )
    {
    printf ( "matched\n" );
    status = 1;
    break;
    }
    }
    if ( status == 0 )
    {
    if (reset_count == 0 )
    {
    printf ( "reset_count = 0\n" );
    lov_reset_values = (char **)malloc( sizeof(char *));
    }
    else
    {
    printf ( "reset_count = %d\n", reset_count );
    lov_reset_values = (char **)realloc( lov_reset_values, sizeof(char *)*(reset_count+1) );
    }
    
    lov_reset_values[reset_count] = (char *)malloc( sizeof( char )*(strlen(str[j])+1));
    strcpy ( lov_reset_values[reset_count], (str)[j] );
    printf ( "lov_reset_values[%d] = %s\n", reset_count, lov_reset_values[reset_count]);
    reset_count = reset_count + 1;
    }
    }
    
    printf (" reset_count = %d\n", reset_count );
    
    /*
    for ( inx = 0; inx < reset_count; inx++ )
    {
    printf ( "lov_reset_values[%d] = %s\n", inx, lov_reset_values[inx] );
    }
    */
    
    return stat;
    }
    Last edited by Salem; 07-28-2009 at 02:08 PM. Reason: Added [code][/code] tags, learn to do it yourself

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. How to complete program. Any probs now?
    By stehigs321 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:03 PM
  5. How do I remove duplicate entries in an array?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-18-2002, 09:49 AM