Thread: how to delete the repeated word

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    3

    how to delete the repeated word

    i have no idea to delete the repeated word and sort the string. Can anyone help me?
    i get the word from infile and generate the outfile which delete the repeated word.
    for example
    infile
    i
    am
    i
    a
    boy

    outfile
    a
    am
    boy
    i

    my code is below. what's wrong? Thx everyone helps me

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct llist                      
    {
                        
       char word[20];                
       struct llist *next;            
    };
    typedef struct llist node;      
    typedef node *llink;
    
    void main()
    {
       
        FILE *infile1 = fopen("wordlist.txt","r");
    	FILE *outfile1 = fopen("wordlist1.txt","w");
    	
    	llink head;                    
        llink ptr;
         
    	int i=0,count=0;
       
        head = ( llink ) malloc(sizeof(node)); 
       head->next = NULL;            
       ptr = head;     
       while(!feof(infile1))  
       {
           
           fscanf(infile1,"%s",ptr->word);     
           ptr->next = ( llink ) malloc(sizeof(node));
           ptr->next->next = NULL;   
           ptr = ptr->next;
    	   count++;
       }
      
       ptr = head;                   
       while(i<count-1)
       {
         fprintf(outfile1,"%s\n",ptr->word);
         ptr = ptr->next; 
    	 i++;
       }
       
      fclose(infile1);
      fclose(outfile1);
     
     
     
    }
    Last edited by icable; 05-11-2003 at 02:51 PM.

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    You really should be using a binary search tree for this, that way you could sort the words and restrict duplicates very very very easily. As it is you'll have to linear search the list for every new word and see if it's already there. If it is then don't insert it.
    p.s. What the alphabet would look like without q and r.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Replies: 8
    Last Post: 01-23-2008, 04:22 AM
  4. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  5. Passing structures to a function
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 07-13-2005, 06:13 AM