Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume that you have a file (named one.txt) that contains an arbitrary number of

ID: 668204 • Letter: A

Question

Assume that you have a file (named one.txt) that contains an arbitrary number of keyvalue pairs (where the key and value are separated by a colon – e.g., x:17). Your script should read the file and create a dictionary that contains all of the keyvalue pairs. Your script should then create two output files: the file two.txt should contain the dictionary keys (one per line); and the file three.txt should contain the dictionary values (one per line). If the input file includes duplicate keys (i.e., the same key appears in multiple places), overwrite the existing value in the dictionary. Count the total number of times that this happens and print the number of unique keys and the number of duplicates on the screen.

Explanation / Answer

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

void main() {
FILE *fp1, *fp2;
char ch;
char dictionary[100][100];
char key[100][50],temp[100];
char value[100][50];
int i=0,j=0,k=0,m=0,n=0,duplicates=0,unique;
clrscr();

fp1 = fopen("one.txt", "r");
if (fp1 == NULL) {
puts("cannot open this file");
exit(1);
}

fp2 = fopen("two.txt", "w");
if (fp2 == NULL) {
puts("Not able to open this file");
fclose(fp1);
exit(1);
}   

fp3 = fopen("three.txt", "w");
if (fp3 == NULL) {
puts("Not able to open this file");
fclose(fp1);
fclose(fp2);
exit(1);
}

while(ch != EOF)
{
       ch = fgetc(fp1);
       if(ch != ' ')
           {
           dictionary[i][j] = ch;
           j++ ;
           }
       else
           {
           i++;
           j = 0;
           }
   }
   for(k=0;k<i;k++)
       {
       for(j=k+1;j<=i;++j)
           {
           if(strcmp(dictionary[k],dictionary[j])>0)
               {
                   strcpy(temp,dictionary[i]);
                   strcpy(dictionary[i],dictionary[j]);
                   strcpy(dictionary[j],temp);
               }
           }
       }
   for(k=0;k<i+1;k++)
       {
       j = 0;
       while(dictionary[k][j] != '')
           {
               if(dictionary[k][j] != ';')
                   {
                   key[k][m] = dictionary[k][j];
                   m++;
                   }
               else  
                   {
                   j++;
                   value[k][n] = dictionary[k][j];
                   n++;
                   }
               j++;
           }  
       }
   for(k=0;k<i;k++)
       {
       for(j=k+1;j<=i;++j)
           {
           if(strcmp(key[k],key[j]) == 0)
               {
                   count++;
                   value[j] = value[i] ;
               }
           }
       }
   unique = i - (2*count) ;
   printf("Number of unique keys = %d", unique);
   printf("Number of duplicate occurences = %d", count);
  
   for (k=0;k<i+1;k++)
   {
       j=0;l=0;
       while(key[k][j] != '')
           {
           fputc(key[k,j],fp2) ;
           j++;
           }
           fputc(' ',fp2) ;
       while(value[k][l] != '')
           {
           fputc(value[k,l],fp3) ;
           l++;
           }
           fputc(' ',fp3) ;
   }
  
fcloseall();
getch();
}