Thread: Anagram C from file

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

    Anagram C from file

    I want to read program which takes word from user and find anagrams for them in file txt. I read this but I have a problem how to read a file and then propertly compare two words. What I do wrong?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    #include <conio.h>
    
    
    bool if_anagram(char *a, char *b)
    {
        
          int dl1 = strlen(a), dl2 = strlen(b);
          
        if(dl1!=dl2)return false;
      
          int licz[127]={}; 
          for(int i=0;i<dl1;i++)
              licz[a[i]]++; 
          for(int i=0;i<dl1;i++)
              licz[b[i]]--; 
              
          for(int i=0;i<127;i++)
              if(licz[i]!=0) 
                return false; 
            
      return true; 
    } 
    
    
    int main(int argc, char *argv[]) {
        
        char a[101], b[8], * buffer; 
        int i=8, n;
        FILE *pFile;
    
    
        pFile=fopen("text.txt","r");
        
        printf("Put word: ");
               scanf ("%s", a);
           
        buffer = (char*) malloc (i+1);
      if (buffer==NULL) exit (1);
            
        if (pFile != NULL){
            while(!feof(pFile))
            {
                 for (n=0; n<i; n++)
                buffer[n]=fgets(b,8,pFile);
                buffer[i]='\0';
            
                printf("%s\n", b);
                //printf("%p\n", &b);
                
                if(if_anagram(a,b))
                     printf("Word %s is anagram!", b);
                     
             free (buffer);    
            }
          fclose(pFile);
      }
      else{
          printf("File is not exist.\n");
      }
      
          
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your buffer is confusing things.
    Code:
    while ( fscanf (pFile,"%s",a) == 1) {
        if(if_anagram(a,b))
            printf("Word %s is anagram!", b);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2018
    Posts
    3
    Ohhh...OK it works now, thanks


    Code:
    #include <stdio.h>
    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    #include <conio.h>
    
    
    bool if_anagram(char *a, char *b)
    {
    
          int dl1 = strlen(a), dl2 = strlen(b);
    
        if(dl1!=dl2)return false;
    
          int licz[127]={}; 
          for(int i=0;i<dl1;i++)
              licz[a[i]]++; 
          for(int i=0;i<dl1;i++)
              licz[b[i]]--; 
    
          for(int i=0;i<127;i++)
              if(licz[i]!=0) 
                return false; 
    
      return true; 
    } 
    
    
    int main(int argc, char *argv[]) {
    
        char a[101], b[8];
        FILE *pFile;
    
    
        pFile=fopen("text.txt","r");
    
        printf("Put word: ");
               scanf ("%s", a);
            
        
        if (pFile != NULL){
        while ( fscanf (pFile,"%s",b) == 1) {
          if(if_anagram(a,b))
            printf("Word %s is anagram!\n", b);
    }
    
          fclose(pFile);
      }
      else{
          printf("File is not exist.\n");
      }
    
    
        return 0;
    }
    
    



    Now I want to write this code in MPI to made this process faster. But I don't know how should it looks like Eny help..please
    Last edited by kanterin; 05-08-2018 at 08:59 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What is your unit of parallelism?
    Each character
    Each word
    Each file?

    The overhead of starting, stopping and synchronising threads will make it slower.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2018
    Posts
    3
    My unit in this program will be each word for several processes.

    I know what it involves, but I wanna try this. How to start?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. anagram strings
    By ph3s in forum C Programming
    Replies: 23
    Last Post: 03-09-2012, 12:58 AM
  2. Help with an Anagram Program
    By Dro in forum C Programming
    Replies: 15
    Last Post: 02-15-2012, 07:34 AM
  3. anagram program help
    By pjr5043 in forum C++ Programming
    Replies: 50
    Last Post: 04-28-2008, 07:32 PM
  4. Anagram
    By vex_helix in forum C Programming
    Replies: 7
    Last Post: 04-05-2004, 12:18 PM

Tags for this Thread