Question

C language

In C language please. 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 a space. Next comes the first name or initial, then a space and the middle initial. Your program should scan the names into three arrays_surname, 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-Montgomery, Jane R Doe J D


0 0
Add a comment Improve this question Transcribed image text
Answer #1
/* C code to process data file and separate names in new file */
// read file name of input and output file from command line
// for example => ./a.out inputfile.txt outputfile.txt
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
  
// as the string contains (" ", "," , ".")
char delim[] = " .,";
char *token;
int i;

char *arrays_surname;
char *first;
char *middle_init;
// opening input file
FILE* file = fopen(argv[1], "r");
char line[80];
FILE *fp;
//opening output file
fp = fopen(argv[2], "w+");   
// getline till end of input file
while (fgets(line, sizeof(line), file))
{
// setting flags for srname, first name ,middle name
int flag = 0;
for (token = strtok(line, delim); token; token = strtok(NULL, delim))
{
// checking flag and putting in output file
if(flag == 0)
{
arrays_surname = token;
flag = 1;
fputs(token, fp);
}
if(flag == 1)
{
first =token;
flag = 2;
fputs(token, fp);
}
if(flag == 2)
{
middle_init = token;
flag = 0;
fputs(token, fp);
}
printf("%s",token);
// spacing the tokens
if(token!=0) { fputs("\t", fp); printf("\t");}
}
// next line for new input
printf("\n");
fputs("\n",fp);
}
// closing files
fclose(file);
fclose(fp);
return 0;
}


answered by: javaeye
Add a comment
Answer #2
#include<stdio.h>
int main()
{
freopen("in.txt" , "r" , stdin);
freopen("out.txt" , "w" , stdout);
int co = 0 , line = 0;
char c , inp[100] = {'\0'};
while(gets(inp))
{
char surname[16] = {'\0'}, first_name[11] = {'\0'}, last_name[11] = {'\0'};
int sidx = 0 , fidx = 0 , lidx = 0;
co++;

int l = strlen(inp) , i = 0 , j = 0, k = 0 , fflag = 0 , sflag = 0;
for(i=0;i<l;i++) {
if(inp[i] == ',') break;
if(sidx >= 15) sflag = 1;
if(sflag == 0) {
surname[sidx] = inp[i];
sidx++;
}
}
surname[sidx] = '\0';
i++; // Increment to avoid space
for(j=i+1;j<l;j++) {
if(inp[j] == ' ') break;
if(fidx >= 10) fflag = 1;
if(inp[j] != '.' && fflag == 0) {
first_name[fidx] = inp[j];
fidx++;
}
}
first_name[fidx] = '\0';
for(k=j+1;k<l;k++) {
if(lidx >= 10) break;
last_name[lidx] = inp[k];
lidx++;
}
last_name[lidx] = '\0';
printf("%s %s %s\n",surname , first_name , last_name);
}
return 0;
}


answered by: javaeye
Add a comment
Know the answer?
Add Answer to:
C language
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that processes a data file of names in which each name is on...

    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. Strings On each line the surname is followed by a comma and a space. Next comes the first name or initial, then a space and the middle initial. Your program should scan the names into three arrays— surname , first , and middle_init . If...

  • I need help with this program using c++ language! CS 317 Program Assignment Name Arrange You...

    I need help with this program using c++ language! CS 317 Program Assignment Name Arrange You are to write a program that will read in each person’s name and print out the results. The list has been collected form different places and the names are not in some standard format. A person’s last name may appear last or it may appear first. A label has been associated with each name to identify where the last name appears. Either “surname” or...

  • C++ Do not use "cin" to get the names from the user. Use "getline()". Name Arranger...

    C++ Do not use "cin" to get the names from the user. Use "getline()". Name Arranger Write a program that asks for the user’s first, middle, and last names. The names should be stored in three different character arrays. The program should then store, in a fourth array, the name arranged in the following manner: the last name followed by a comma and a space, followed by the first name and a space, followed by the middle name. For example,...

  • In C Write a function that asks for the user's first, middle, and last names. The...

    In C Write a function that asks for the user's first, middle, and last names. The names will be entered by the user on a single line and will be stored in three different C-strings. The program should then store, in a fourth array, the name arranged in the following manner: the last name followed by a comma and a space, followed by the first name and a space, followed by the middle name. For example, if the user entered...

  • C++ A professor plans to store the following data for each of his students: Last Name,...

    C++ A professor plans to store the following data for each of his students: Last Name, First Name, Class Average (as a double), Letter Grade (“A”, “A-“, “B+”, “B”, etc.) Write two programs for writing and reading such a file, with the user choosing the file name. Store the (int) number of students as the first data value in the file, followed by \n. Then use the following delimiter scheme: Last Name(comma)First Name(comma)Average(space)Letter Grade(newline) For the file writer program, input...

  • Write a C program to compute average grades for a course. The course records are in...

    Write a C program to compute average grades for a course. The course records are in a single file and are organized according to the following format: Each line contains a student’s first name, then one space, then the student’s last name, then one space, then some number of quiz scores that, if they exist, are separated by one space. Each student will have zero to ten scores, and each score is an integer not greater than 100. Your program...

  • This is for a class in C++ Problem 1: A class Names reads the following names...

    This is for a class in C++ Problem 1: A class Names reads the following names from the .txt file Name.txt [Insert a long list of first, middle and last names. Each set of names is placed on a different line like the following: John Doe Jane Doe Johnie Boy etc.] Make a search directory using dynamic allocation (pointers) where the user would search the any number of letters of the first name and the matching name/names would come up...

  • Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...

    Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...

  • Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX...

    Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX_NAME_LENGTH]; int customer_number[N_CUSTOMERS] A program uses a text file that contains the following data on each line: The customer number is an int, and the first and last names are alphabetic strings that contain no whitespace. The last and first names themselves are however separated by whitespace. Write a C function with the following prototype: void read_customer (char name[][MAX_NAME_LENGTH], int number[], int position, FILE *cust_file) Your function should read a...

  • write program in C language. To get more practice working with files, you will write several...

    write program in C language. To get more practice working with files, you will write several functions that involve operations on files. In particular, implement the following functions 1. Write a function that, given a file path/name as a string opens the file and returns its entire contents as a single string. Any endline characters should be preserved char *getFileContents (const char filePath); 2. Write a function that, given a file path/name as a string opens the file and returns...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT