Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Imladris improvement algorithm-compname()

Dear imladris,

as the first question of mine if you could stil remember :
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cprog&qid=10345372
I would like to improve and make a few changes..
my requirement is to change this program into the last one you made for me...

compare name with datafile (names.dat) and then print the name matches and then return  number of matched names...get ird of the output header and all information like death date, marriage, last name , first name..only keep name  to compare with datafile...(anyway you would understand this easily since it's eaxtly the same requirements as you have done be4)

many thanks,
Korsila

p.s. I need to think for a good requirement to challenge your skill :) and i would be deluighted if you coud move your position of expert to second  ^_^
hope to see your pix one day wining expert of the month heheheh


0
korsila
Asked:
korsila
1 Solution
 
imladrisCommented:
This should do it:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int compname(char *,char *);
int compchar(char a,char b);
void getinput(char *prompt,char n[]);

void main(int argc,char *argv[])
{   int matched,msave,usave,mc;
  char name[150],line[500];
  float LIG;
  FILE *namefile,*mfile,*ufile;

  printf("Save matched names in matched.dat (y or n)?\n");
  gets(line);
  msave=(toupper(line[0])=='Y');
  printf("Save unmatched names in unmatched.dat (y or n)?\n");
  gets(line);
  usave=(toupper(line[0])=='Y');
  namefile=fopen("names.dat","r+"); //open file in read/write mode
  if(msave)mfile=fopen("matched.dat","w");
  if(usave)ufile=fopen("unmatched.dat","w");
  do
  {   printf("Please enter name to match:\n");
      gets(name);
      fseek(namefile,0L,0); //seek to start of file
      matched=0;
      mc=0;
      while(fgets(line,500,namefile)!=NULL)
      {   line[strlen(line)-1]='\0';
          if(compname(name,line))
          {    printf("%s\n",line);
               ++mc;
               if(msave)
               {   if(matched==0)fprintf(mfile,"Match name %s\n",name);
                   fprintf(mfile,"%s\n",line);
               }
               matched=1;
          }
      }
      if(matched==1)
       {   if(msave)fprintf(mfile,"----------------------------------------\n");
           printf("Number of Matches: %d\n",mc);
       }
      else
      {   printf("No Matches Found.\n");
          if(usave)
          {   fprintf(ufile,"Match name %s\n",name);
              fprintf(ufile,"No Matches found\n");
              fprintf(ufile,"--------------------\n");
          }
      }
      printf("Compare again (y or n)?\n");
      gets(line);
  } while(toupper(line[0])=='Y');
  fclose(namefile);
  if(msave)fclose(mfile);
  if(usave)fclose(ufile);
}

int compname(char *nm1,char *nm2)
{   int i,l1,l2;

   l1=strlen(nm1);
   l2=strlen(nm2);
   for(i=0; i<l1; ++i)
   {   if(i<l2 && compchar(nm1[i],nm2[i]))continue;
       if(i+1<l2 && compchar(nm1[i],nm2[i+1]))continue;
       if(i+2<l2 && compchar(nm1[i],nm2[i+2]))continue;
       if(i-1<l2 && i>0 && compchar(nm1[i],nm2[i-1]))continue;
       if(i<l2 && i>0 && compchar(nm1[i-1],nm2[i]))continue;
       if(i+1<l1 && i<l2 && compchar(nm1[i+1],nm2[i]))continue;
       if(i+2<l1 && i<l2 && compchar(nm1[i+2],nm2[i]))continue;
       if(i+1<l1 && i+1<l2 && compchar(nm1[i+1],nm2[i+1]))continue;
       if(i+2<l1 && i+2<l2 && compchar(nm1[i+2],nm2[i+2]))continue;
       return(0);
   }
   return(1);
}

int compchar(char a,char b)
{     return(toupper(a)==toupper(b));
}


// prompt user for input
// get input
// copy it safely into provided variable

void getinput(char *prompt,char n[])
{   char ipc[150];

  printf("%s:\n",prompt);
  gets(ipc);
  strncpy(n,ipc,19);
  n[19]='\0';
  return;
}
0
 
korsilaAuthor Commented:
thanks...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now