Question

In: Computer Science

Language C: Suppose you are given a file containing a list of names and phone numbers...

Language C: Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file.

Example input/output:
Enter the file name: input_names.txt
Output file name: phone_input_names.txt

1) Name your program phone_numbers.c

2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume the length of each line in the input file is no more than 10000 characters.

3) The program should include the following function: void extract_phone(char *input, char *phone); The function expects input to point to a string containing a line in the “First_Last_Phone” form. In the function, you will find and store the phone number in string phone.

Solutions

Expert Solution

I had updated the answer. Please check the code :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 10000
void extract_phone(char *input, char *phone)
{
    char *temp;
    strcpy(phone, "");
    // Split the tring ,when the first "_" was found
    temp = strtok(input, "_");
    // Repeat the split for two times to get the last string
    temp = strtok(NULL, "_");
    temp = strtok(NULL, "_");
    // copy that  string to the pointer phone
    strcpy(phone, temp);
}
int main()
{
    char buffer[MAX_LEN];
    char phone[MAX_LEN];
    char filename[100], outputfilename[100] = "phone_";
    printf("\nEnter the file name : ");
    scanf("%s", filename);
    strcat(outputfilename, filename);
    printf("Output file name : %s\n", outputfilename);

    FILE *fp;
    fp = fopen(filename, "r");
    if (fp == NULL)
    {
        perror("Failed to open the input file: ");
        return 1;
    }
    FILE *fq;
    fq = fopen(outputfilename, "w");
    if (fq == NULL)
    {
        perror("Failed to open the output file: ");
        return 1;
    }

    // -1 to allow room for NULL terminator for really long string
    while (fgets(buffer, MAX_LEN - 1, fp))
    {
        // Remove trailing newline
        buffer[strcspn(buffer, "\n")] = 0;

        extract_phone(buffer, phone);
        // Write the returned value to the file
        fprintf(fq, "%s\n", phone);
    }

    fclose(fp);
    fclose(fq);
    return 0;
}

Here the file will be created with only the phone numbers. And the file name will be as specified in the question, ("ie. phone_input_names.txt")

As in the question, I had not printed any other details, than specified in the question, To see the file , just open the output file


Related Solutions

Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." In C, Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume the length...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C language to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters....
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
C++ language. Suppose you are given a list of students registered in a course and you...
C++ language. Suppose you are given a list of students registered in a course and you are required to implement an Linked List of student. Each student in the list has name, regNo and cGpa. You task is to implement following function related to Linked List. insertStudent() : This function, when called will prompt user to enter his/her choice of insertion. The options are 1. Insert student at the start of linked list 2. Insert student at the end of...
Creating a list/tuple 3.Given a list of tuples containing student first names and last names e.g....
Creating a list/tuple 3.Given a list of tuples containing student first names and last names e.g. (“John”, “Doe”) that represents a class. Ask the user for a students first and last name and check if that student is a member of the class.
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
(C++) You are given a file consisting of students’ names in the following form: lastName, firstName...
(C++) You are given a file consisting of students’ names in the following form: lastName, firstName middleName. (Note that a student may not have a middle name.) Write a program that converts each name to the following form: firstName middleName lastName. Your program must read each student’s entire name in a variable and must consist of a function that takes as input a string, consists of a student’s name, and returns the string consisting of the altered name. Use the...
(c++)You will be given a data file containing data for 10 students. The format is as...
(c++)You will be given a data file containing data for 10 students. The format is as follows - grades are double precision numbers: Line 1: Header Information Line 2: Student Full name Line 3: Student ID Line 4: testgrade_1 testgrade_2 testgrade_3 testgrade_4 testgrade_5 Line 5: Student Full name Line 6: Student ID Line 7: testgrade_1 testgrade_2 testgrade_3 testgrade_4 testgrade_5 Line 8: Student Full name Line 9: Student ID Line 10: testgrade_1 testgrade_2 testgrade_3 testgrade_4 testgrade_5 Etc. Read the data into...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT