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

Please also comment on your code to help to understand, thank you.

Solutions

Expert Solution

The program is given below: that take input file name from user, set output file name by adding phone_ at the beginning of ip_file, then open files, read input file line by line and call extract_phone() function that find & store the phone number into phone and  write phone into output file, then close file.

phone_numbers.c

#include <stdio.h>
#include<stdlib.h>
#include <string.h>
//extract_phone() function find & store the phone number into phone
void extract_phone(char *input, char *phone)
{
char *token=strtok(input,"_");
while(token!=NULL)
{
//copy token into phone
strcpy(phone,token);
token=strtok(NULL,"_");
}
}
int main()
{
char ip_file[100],op_file[106]="phone_";
char line[10000];
char phone[10];
//create FILE pointer
FILE *fp=NULL,*fp_op=NULL;
  
//take input file name from user
printf("Enter the file name: ");
scanf("%s",ip_file);
  
//set output file name by adding phone_ at the beginning of ip_file
strcat(op_file,ip_file);
printf("Output file name: %s\n",op_file);
  
//open file in read mode
fp=fopen(ip_file,"r");
//open file in write mode
fp_op=fopen(op_file,"w");
  
//read file line by line
while(fgets(line,10000,fp))
   {//call extract_phone() function that find & store the phone number into phone
extract_phone(line,phone);
//write phone into output file
fprintf(fp_op,"%s",phone);
}
  
//close file
fclose(fp);
fclose(fp_op);
return 0;
}

The screenshot of code is given below:

input_names.txt:

abc_abcde_3893274982
sd_dff_3289824908
dsd_dcn_3298320820
wxy_abc_3287987789

Output:

phone_input_names.txt (after execution of program):

3893274982
3289824908
3298320820
3287987789


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...
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 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...
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...
In this assignment you will be given an input file containing a series of universities along...
In this assignment you will be given an input file containing a series of universities along with that universities' location and rating. You must write a program that uses user defined functions to output a list of all school's above a certain rating to the terminal. Your program should do the following: - Prompt the user for the name of the input file to open - Prompt the user for the rating threshold ( Note: we print if the rating...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT