Ask C/C++ Expert


Home >> C/C++

PA #1: Word Counter

Tabulating basic document statistics is an interesting exercise that leverages your knowledge of strings, files, loops, and arrays. In this homework, you must write a C++ program that asks the user for an input and output file. For each line in the input file, write a modified line containing a line number to the output file. Additionally, calculate the number of paragraphs, lines, words, and characters. Write the summary information to the bottom of the output file.

Program Flow

Your program should operate as follows:

• Prompt the user for an input and output file
• Loop through the input file

o Increment the current line number
o If the line contains only a single newline character (\n), it is considered to be a paragraph. Increment the number of paragraphs
o Add the line's number of characters with spaces, characters without spaces, and word count to the document total.

- To make things simpler, use spaces to determine word boundaries. For example "quick thinking" is two words whereas "quick-thinking" is one.

- Remember to not count newlines '\n' as characters in any circumstances

o Write the current line number plus the line to the output file

• Write the following statistics at the bottom of the output file:

o paragraphs
o words
o characters (not counting spaces)
o characters (counting spaces)
o total lines

Required Functions

In all, you must define and use the following functions:

int get_character_count_with_spaces(string line);

This returns the total number of characters (including spaces) present in the given character array. Be sure not to count newline characters '\n' in this count.

int get_character_count_without_spaces(string line);

This returns the total number of characters (not including spaces) present in the given character array. Be sure to not count newline '\n' characters and spaces ' '.

int get_words(string line);

This returns the total number of words present in the given character array. For simplicities sake, words are defined by spaces. For example "quick thinking" is two words whereas "quick-thinking" is one.

int is_paragraph(string line);

This returns a value of 1 if the current line is a paragraph (only contains a newline '\n') and 0 otherwise.

void parse_file(ifstream input_file, ofstream output_file);

This function accepts input and output files and should be responsible for performing file calculations via the other functions. This is also where you will output the modified document with line numbers.

Sample Output

Your program's output should match the following:
Please enter a file to parse: savio.txt
Please enter a destination file: savio_parsed.txt
Program Complete.

The contents of the outputted file (in this case, savio_parsed.txt) would then look like:

1 We have an autocracy which runs this university. It's managed.
2 We asked the following: if President Kerr actually tried to get something more
3 liberal out of the Regents in his telephone conversation, why didn't he make
4 some public statement to that effect? And the answer we received -- from a
5 well-meaning liberal -- was the following: He said, "Would you ever imagine
6 the manager of a firm making a statement publicly in opposition to his board
7 of directors?" That's the answer! Now, I ask you to consider: if this is a
8 firm, and if the Board of Regents are the board of directors, and if President
9 Kerr in fact is the manager, then I'll tell you something: the faculty are a
10 bunch of employees, and we're the raw material! But we're a bunch of raw
11 material[s] that don't mean to have any process upon us, don't mean to be made
12 into any product, don't mean to end up being bought by some clients of the
13 University, be they the government, be they industry, be they organized labor,
14 be they anyone! We're human beings!
15
16 There is a time when the operation of the machine becomes so odious, makes you so
17 sick at heart, that you can't take part; you can't even passively take part,
18 and you've got to put your bodies upon the gears and upon the wheels, upon the
19 levers, upon all the apparatus, and you've got to make it stop. And you've got
20 to indicate to the people who run it, to the people who own it, that unless
21 you're free, the machine will be prevented from working at all!

paragraphs: 1
words: 271
characters (no spaces): 1206
characters (with spaces): 1457
total lines: 21

Test Cases

You have been provided with two test cases (savio.txt / savio_parsed.txt; kennedy.txt / kennedy_parsed.txt). Note I am allowing a tolerance of 10% from my results per statistic so your results don't have to be 100% spot-on.

C++ File, Header Comment, and Formatting

1. Be sure to modify the file header comment at the top of your program to indicate your name, student ID, completion time, and the names of any individuals that you collaborated with on the assignment.

2. Remember to follow the basic coding style guide. For a list of basic rules, see my website or examine my example files from previous assignments and labs.

Reflection Essay

In addition to the programming tasks listed above, your submission must include an essay that reflects on your experiences with this homework. This essay must be at least 350 words long. Note that the focus of this paper should be on your reflection, not on structure (e.g. introductory paragraph, conclusion, etc.). The essay is graded on content (i.e. it shows deep though) rather than syntax (e.g. spelling) and structure. Below are some prompts that can be used to get you thinking. Feel free to use these or to make up your own.

• Describe a particular struggle that you overcame when working on this programming assignment.
• Conversely, describe an issue with your assignment that you were unable to resolve.
• Provide advice to a future student on how he or she might succeed on this assignment.
• Describe the most fun aspect of the assignment.
• Describe the most challenging aspect of the assignment.
• Describe the most difficult aspect of the assignment to understand.
• Provide any suggestions for improving the assignment in the future.

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M91963650
  • Price:- $70

Priced at Now at $70, Verified Solution

Have any Question?


Related Questions in C/C++

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As