Question

This assignment tests your ability to write C programs that handle keyboard input, formatted console output,...

This assignment tests your ability to write C programs that handle keyboard input, formatted console output, and input/output with text files. The program also requires that you use ctype functions to deal with the logic.

In this program, you will input from the user two characters, which should both be letters where the second letter > the first letter. You will then input a file character-by-character and output those letters that fall between the two characters (inclusively) to an output file, along with all white space, digits and the punctuation marks of period and comma, but no other punctuation marks. The two letters can be input in either case, but test both upper and lower case versions when deciding whether a character is output or not. For instance, if the letters are ‘b’ and ‘p’, then anything that is >= ‘b’ and <= ‘p’ or >= ‘B’ and <= ‘P’ will be output. To ensure that the two characters are letters and the second is > first, use a data verification loop like

do {

input both characters

}while(!isalpha(first)||!isalpha(second)||second <= first);

NOTE: if you input first and second as chars, you might find an odd behavior if you have to input them a second time, or beyond. That is, if first or second is not a letter, or second <= first, re-executing the scanf in the above do-while loop causes an odd behavior. To avoid this, you can input two int values and then cast them into chars as follows:

scanf(“%d %d”, …) ;

first = (char) …;

second = (char) …;

If you use this code, you will have to input the letters using their ASCII values, such as 97 for ‘a’. You may implement this however you like, and when you run it, if you input legal chars for both first and second, you will avoid this problem.

In addition to outputting all of the characters that qualify (fall between the two input chars inclusively, is white space, digit, period or comma), count each input character, each output character, and the number of letters that were not output. After exiting the while loop, close both files and compute the difference in size between the output file and input file, or output / input * 100. Notice that output and input should be int variables, but since this is an integer division, and output < input, you need to perform a proper cast or your result will be 0 (or 0.0). Store the percentage in a double. Output a short report to the console window (using printf statements) that lists the input file name, output file name, size of input file, size of output file, number of letters not output, and percentage file change. See the example output at the bottom of the assignment.

There are two input files on the website. Download them both. Run your program on the message1.txt input file using letters ‘c’ and ‘m’. The output you should obtain is shown on the next page. Once you have this running correctly, rerun your program on the same input file using letters ‘e’ and ‘s’. Next, run the program on the second input file (message2.txt) with letters ‘b’ and ‘h’. Submit your program (commented), the console window output from the runs (first input file on ‘e’…‘s’, second input file on ‘b’…‘h’) and the two output files created (message1.txt on ‘e’ and ‘s’, message2.txt on ‘b’ and ‘h’). To obtain the console window output, obtain a screen

capture (alt+printscreen) or copy and paste the text from the console window. All five of these items (program, console output for message1.txt, output file for message1.txt, console output for message2.txt, output file for message2.txt) can be either placed in a single zip/rar file and emailed or all copied into a single text file, word document or pdf, and sent that way. Email your submission to [email protected] by 2 pm on Monday, May 20. Late assignments are excepted with a late penalty.

Input:

Enter the two letters: c m

Input file name: message1.txt

Output file name: message1out.txt

Input file size: 158

Output file size: 80

Letters not output: 74

Difference in Size: 50.6%

Created text file (message1output.txt):

Ifmi i kledge

kledge i idm,

idm i h,

h i e,

e i le,

le i mic.

MIC i he E

message1.txt input:

Information is not knowledge;
knowledge is not wisdom,
wisdom is not truth,
truth -- is not beauty,
beauty is not love,
love is not music.
MUSIC is the BEST!

message2.txt input:

Information is not knowledge;
knowledge is not wisdom,
wisdom is not truth,
truth -- is not beauty,
beauty is not love,
love is not music.
MUSIC is the BEST!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include<stdio.h>

#include<ctype.h>

int main(){

char ch1, ch2;

char inp_file[50], out_file[50];

printf("Enter two letters: ");

scanf("%c %c", &ch1, &ch2);

printf("Input file name: ");

scanf("%s", inp_file);

printf("Output file name: ");

scanf("%s", out_file);

FILE *fp;

fp = fopen(inp_file, "r");

if(fp==NULL){

printf("Unable to open file\n");

return -1;

}

FILE *fout;

fout = fopen(out_file, "w");

int inp_size=0, out_size=1, letters_not=0;

char line[1000], ch;

while(!feof(fp)){

fscanf(fp, "\n%[^\n]s", line);

int i=0;

while(line[i]!='\0'){

inp_size++;

ch = line[i];

if(isalpha(ch) || ch==' ' || ch==',' || ch=='\n'){

if((toupper(ch)>=toupper(ch1) && toupper(ch)<=toupper(ch2)) || ch==' ' || ch==',' || ch=='\n'){

fprintf(fout, "%c", ch);

out_size++;

}

else

letters_not++;

}

i++;

}

inp_size++;

fprintf(fout, "\n");

out_size++;

line[0] = '\0';

}

printf("Input file size: %d\n", inp_size);

printf("Output file size: %d\n", out_size);

printf("Letters not output: %d\n", letters_not);

printf("Difference in Size: %.1f%%\n", (((float)out_size/inp_size)*100));

fclose(fout);

fclose(fp);

return 0;

}

Add a comment
Know the answer?
Add Answer to:
This assignment tests your ability to write C programs that handle keyboard input, formatted console output,...
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
  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • JAVA 6. Create two files and name them: puzzle.txt and puzzle2.txt Inside puzzle.txt, write the following...

    JAVA 6. Create two files and name them: puzzle.txt and puzzle2.txt Inside puzzle.txt, write the following text: MWTaahyiebt_e,c__hnyaoontuc;'e_rste_r_aynr_oert_e_gasoduoipdnp_got_shoeandtl__yty_oot_uhrree__apTdrH_oItgRhrDia_sml__eowtnotere.kr_ss_. Inside puzzle2.txt, write the following text: WTTohhriikssi__niigss,___ttbhhueet___wryrioogunh'gtr__emm_eessshssoaawggieen__gff_rrtoohmme___sswaarmmoppnllgee_22o..nttexxstt Open a file specified by the user. This file will contain a bunch of characters. You should read in each character from the file, one character at a time. Display every third character on the screen. Throw the other characters away. There is a sample input file called puzzle.txt, containing a little message you can...

  • C++ There are to be two console inputs to the program, as explained below. For each input, there is to be a default val...

    C++ There are to be two console inputs to the program, as explained below. For each input, there is to be a default value, so that the user can simply press ENTER to accept any default. (That means that string will be the best choice of data type for the console input for each option.) The two console inputs are the names of the input and output files. The default filenames are to be fileContainingEmails.txt for the input file, and...

  • This is a c++ question note: not using  namespace std; at the beginning of the program Writing...

    This is a c++ question note: not using  namespace std; at the beginning of the program Writing Data to a File This program will write a series of letters, starting with 'A', to an external file (letters.txt). The user will decide how many letters in total get saved to the file. ** IMPORTANT: The test cases will evaluate your code against .txt files that I uploaded. You do NOT have to upload your own txt files. Input: Including 'A', how many...

  • C++ program: The aim of this homework assignment is to practice writing hierarchy of classes. Design...

    C++ program: The aim of this homework assignment is to practice writing hierarchy of classes. Design a hierarchy of Files. You have two different kinds of Files, Text File and an Image File. The files are identified by their name and type, which is identified by either txt or gif extension. An Image file has dimensions of pixel rows and pixel columns. Each pixel has a color depth that can be represented by number of bits. (8 bits is one...

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

  • Write a python program that prompts the user for the names of two text files and...

    Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...

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