Question

In: Computer Science

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 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.

Comments would be helpful as well Thanks

Solutions

Expert Solution

Here is the Code Snippet and Screenshots of input and output are given.

// C Program to extract phone numbers
#include <stdio.h>
#include <string.h>
// extrating phone number
void extract_phone(char *input, char *phone)
{
    int j = 0;
    int n = strlen(input);
    // as phone number is as last so traversing from end
    for (int i = n - 1; input[i] != '_'; i--)
    {
        phone[j] = input[i];
        j++;
    }
    phone[j] = '\0';
    n = strlen(phone);
    // reversing the phone number as we have extracted from the end
    for (int i = 0; i < n / 2; i++)
    {
        char temp = phone[i];
        phone[i] = phone[n - i - 1];
        phone[n - i - 1] = temp;
    }
}
int main()
{
    char input_file[100], output_file[100];
    printf("Enter the input file name : ");
    gets(input_file);
    printf("Enter the output file name : ");
    scanf("%s", &output_file);
    // file handling
    FILE *filePointer1, *filePointer2;
    filePointer1 = fopen(input_file, "r");
    if (filePointer1 == NULL)
    {
        printf("Unable to open file . Check if it is in the same folder as program !");
    }
    else
    {
        filePointer2 = fopen(output_file, "w");
        char input[1000], phone[50];
        int f = 0;
        while (fgets(input, 1000, filePointer1) != NULL)
        {

            extract_phone(input, phone);
            fputs(phone, filePointer2); // writing to output file
        }
        // Closing the file using fclose()
        fclose(filePointer1);
        fclose(filePointer2);
    }
    return 0;
}


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 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....
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...
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)
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.
Suppose you have a list containing k integer numbers. Each number could be positive, negative, or...
Suppose you have a list containing k integer numbers. Each number could be positive, negative, or zero. Write pseudocode for a program that first asks the user for each of the numbers. After that, it should determine and display the number from the list that is nearest to the positive number five (5). Notes: 1. Try working this out for k = 4 and k = 5. See if you can find an algorithmic pattern that can be extended for...
Below is a small part of the phone list of B. Obama. The phone numbers and...
Below is a small part of the phone list of B. Obama. The phone numbers and addresses have been left out here. a) Show with binary search how you would look up the person Casy in this list (manually). b) Explain that linear search, in this case, is faster than binary search. Phone List (1) Allen (2) Baley (3) Boyer (4) Casy (5) Davis (6) Davison (7) Glen (8) Greer (9) Haley (10) Hanson (11) Harrison (12) Lister (13) Mendel...
Suppose a file (whose name is given through an argument by the user) contains student names...
Suppose a file (whose name is given through an argument by the user) contains student names and their scores (ranging from 0 to 100): i.e. each line contains a student name (just the first name with no space inside) and the student’s score separated by a space. Find out who has the highest score with what score, and who has the lowest score with what score. Also, calculate the average of all of the scores. Do not use arrays to...
Suppose a file (whose name is given through an argument by the user) contains student names...
Suppose a file (whose name is given through an argument by the user) contains student names and their scores (ranging from 0 to 100): i.e. each line contains a student name (just the first name with no space inside) and the student’s score separated by a space. Find out who has the highest score with what score, and who has the lowest score with what score. Also, calculate the average of all of the scores. Do not use arrays to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT