Write a computer program as a Win32 console application in C

Write a computer program as a Win32 console application in C to process a text data fileof names in which each name is on a separate line of at most 80 characters. See twosample names below:

Hartman-Montgomery, Jane R.Doe, J. D.

On each line the surname is followed by a comma and a space. Next comes the first nameor initial, then a space and the middle initial. Your program should scan the names intothree arrays - surname, first, and middle_init. If the surname is longer than 15characters, store only the first 15. Similarly, limit the first name to ten characters. Do notstore periods in the first and middle_init arrays. Write the array’s contents to a
file named names_output.txt, aligning the contents of each column, as shownbelow for the sample data above:

Hartman-Mongom Jane R

Doe J D

Solution

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

int main()
{
FILE* fp;
char line[255];
   char first[15],last[15],surname[15];
   int i=0,j;
fp = fopen(\"input.txt\" , \"r\");
while( fgets(line, sizeof(line), fp) != NULL ){ // reading from file
char *position;
if ((position=strchr(line, \'\ \')) != NULL)
*position = \'\\0\';
strcpy(first[i] , strtok(line,\",\"));
       strcpy(line, strtok(line,\",\"));
strcpy(last[i] , strtok(line,\" \"));
       strcpy(surname[i] , strtok(NULL,\" \"));   
       i++;
   }
   // writing to this file  
   FILE *f = fopen(\"names_output.txt\", \"w\");
   if (f == NULL)
   {
       printf(\"Error in opening file!\ \");
       exit(1);
   }
   for(j=0;j<i;j++){
       fprintf(f, \"%s %s %s\ \", first[i],last[i],surname[i]); // saving to file
   }
}

Write a computer program as a Win32 console application in C to process a text data fileof names in which each name is on a separate line of at most 80 characte

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site