#include <string.h>
#include <stdlib.h>
#include <stdio.h>
/* uni() function takes an array an thier size.and it produce an array which is include unique element */
int uni(char *arr[],int size)
{
int unique = 0; /* The length of dst after removing duplicates */
int n=size;
char *dst[n]; /* The set of stirings without duplicates */
int i;
/* The first number is never a duplicate */
dst[unique++] = arr[0];
/* Load the unique strings into the destination list */
for ( i = unique; i < n; i++ ) {
int has_dup = 0;
int j;
for ( j = 0; j < unique; j++ ) {
if ( arr[i] == dst[j] )
has_dup = 1;
}
if ( has_dup == 0 )
dst[unique++] = arr[i];
}
/* Display the unique strings*/
for ( i = 0; i < unique; i++ )
printf ( "%s ", dst[i] );
printf ( "\n" );
return 0;
}
int main ( void )
{
static const char filename[] = "input1.txt";
char name[20];
char *image[1000];
int i=0,h,x,y,w,j=0;
/*Malloc for image*/
for(j=0;j<1000;j++)
image[j] = malloc(sizeof(char *)*10);
/*End Malloc*/
FILE *file = fopen ( filename, "r" );
if ( file != NULL )
{
char line [ 128 ];
while ( fgets ( line, sizeof line, file ) != NULL ){
sscanf(line,"%s%d%d%d%d%s",name,&w,&h,&x,&y,image[i++]);
}
fclose ( file );
}
else
{
perror ( filename ); /* why didn't the file open? */
}
uni(image,1000);
return 0;
}
1) uni() function for making a unique array
2) the main function read a txt file which is include
rectangle 0 0 1 1 orange
rectangle 0 1 1 1 green
rectangle 0 2 1 1 white
rectangle 0 3 1 1 orange
rectangle 0 4 1 1 white
rectangle 0 5 1 1 black
rectangle 0 6 1 1 blue
rectangle 0 7 1 1 red
rectangle 0 8 1 1 blue
rectangle 0 9 1 1 white
rectangle 0 10 1 1 green
rectangle 0 11 1 1 green
something like that (1000 or more line ).
then i want to filter only colour name without duplicating. But my program doesn't work .it produce all colour (with duplicate colour name).. I tested uni() function it works properly. Why my program doesn't work properly (i want to print out only colour name without duplicated )..