Tuesday, July 14, 2020

#73 Write a program that processes a data file of names


Program Language Free Chegg Q&A


Chemistry Explain is here for you to provide the important questions and answer “#73 Write a program that processes a data file of names” this question is coming from Computer Science.
#73 Write a program that processes a data file of names
Get the Free Online Chemistry Q&A Questions And Answers with explain. To crack any examinations and Interview tests these Chemistry Questions And Answers are very useful. Here we have uploaded the Free Online Chemistry Questions. Here we are also given the all chemistry topic.

ChemistryExplain team has covered all Topics related to inorganic, organic, physical chemistry, and others So, Prepare these Chemistry Questions and Answers with Explanation Pdf.

Question


Write a program that processes a data file of names in which each name is on a separate line of at most 80 characters. Here are two sample names:

Hartman-Montgomery, Jane R.

Doe, J. D.

On each line, the surname is followed by a comma and space. Next comes the first name or initial, then space and the middle initial. Your program should scan the names into three arrays— surnames, first, and middle_init. If the surname is longer than 15 characters, store only the first 15. Similarly, limit the first name to ten characters. Do not store periods in the first and middle_init arrays. Write the array’s contents to a file, aligning the contents of each column:

Hartman-Montgom Jane RDoe JD

Answer


Step 1

Program:

/**********************************************************

*Program to process a data file of names on a separate *

*line and print the required in an output file *

**********************************************************/

#include

#include

#include

//define the size as 50.

#define SIZE 50

int main()




{

  //declare the variables.

  char line[80]=" ";

  int count =0;

  //declare three arrays

  char first[SIZE][11];

  char surname[SIZE][16];

  char middle_init[SIZE][11];

  //open the inout file in read mode.

  FILE *fin = fopen("input.txt", "r");

  //if file not open, print a message.

  if(fin==NULL)

  {

    printf("Unable open the input file.\n");

  }

  else

  {

    //open the output in write mode.

    FILE *fout = fopen("output.txt", "w");

    //if file not open, print a message.

    if(fout==NULL)

    {

      printf("Cannot open the output file.\n");

      fclose(fin);

    }

    else

    {

      //read the file until end of file.

      while(fgets(line, 80, fin)!=NULL)

      {

        //declare the variables

        int i=0;

        char temp[]=" ";

        char *tempStr = strtok(line, temp);

        //read the surname upto less than or

        //equal to 15 skip the commas

while((i<15)&& *(tempStr+i)!=','&& *(tempStr+i)!='\n')

        {

          surname[count][i]=*(tempStr+i);

          i++;

        }

        surname[count][i]='\0';

        tempStr = strtok(NULL, temp);

        //read the first name upto less than or

        //equal to 10 skip the commas

        i=0;

while((i<10)&&*(tempStr+i)!='.'&&*(tempStr+i)!=','&& *(tempStr+i)!='\n')

        {

          first[count][i]=*(tempStr+i);

          i++;

        }

        first[count][i]='\0';

        tempStr = strtok(NULL, temp);

        i=0;

        //read the middile name upto less than

        //or equal to 10 skip the commas

while((i<10)&&*(tempStr+i)!='.'&&*(tempStr+i)!=','&& *(tempStr+i)!='\n')

        {

               middle_init[count][i]=*(tempStr+i);

          i++;

        }

        middle_init[count][i]='\0';

        //write the data in the output file.

Step 2

fprintf(fout,"%-15s\t\t%-10s\t%-10s\n",surname[count], first[count], middle_init[count]);

        //increment the count value.

        count++;

      }

    }

    //close the output and input files.

    fclose(fout);

    fclose(fin);

  }

  return 0;

}

Step 3

Input text file: input.txt

Hartman-Montgomery, Jane R.

Doe, J. D.

Step 4

Sample output file: output.txt


Picture 1

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home