Given a string "how are you how", you have to remove repeating word which is "how" and should output the remaining unique words as "how are you".
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdlib.h>
/* Getting index value(ascii sum of that word) */
#define STR_INDEX(x) ((int)x-(int)'a')
#define MAX 100000
int main(void) {
char *s="abc def ghi jkl"; //Input string
char *b[4];
char *c[4];
int index;
int i,k;
int hash[MAX];
static int j=0;
/* Set the memory of hash to zero */
memset(hash,0,sizeof(hash));
/* Allocate memory for Two-Dimensional Arrays */
for(i=0;i<4;i++)
b[i]=(char*)malloc(sizeof(char));
for(i=0;i<4;i++)
c[i]=(char*)malloc(sizeof(char));
/* Split the words from 1-D and copy to 2-D */
for(i=0;i<4;i++)
{
for(k=0;*(s+j)!=' ';j++,k++)
{
*(*(b+i)+k)=*(s+j);
}
*(*(b+i)+k)='\0';
j++;
}
/*for(i=0;i<4;i++)
printf("%s\n",*(b+i));
*/
i=0;
/* check if the hash index is already been set */
/* If not, copy the word to another array 'c'*/
while(i<4)
{
index=0;
for(j=0;j<strlen(b[i]);j++)
index+=STR_INDEX(*(*(b+i)+j));
if(hash[index]==0)
{
/* Un comment the following if you do not want to use strcpy function */
/*for(j=0;j<strlen(b[i]);j++)
*(*(c+i)+j)=*(*(b+i)+j)*/
strcpy(c[i],b[i]);
/*
*(*(c+i)+j)='\0';
*/
}
/* Set the hash index to value 1 */
hash[index]=1;
i++;
}
/* Finally print the output strings which are unique */
for(i=0;i<4;i++)
printf("%s\n",*(c+i));
return 0;
}