In: Computer Science
How many words are in the Gettysburg Address?
Write a program that reads any text file, counts the number of characters, num- ber of letters and number of words in the file and displays the three counts.
To test your program, a text file containing Lincoln’s Gettysburg Address is included on the class moodle page.
Sample Run
Word, Letter, Character Count Program
Enter file name: GettysburgAddress.txt
Word Count = 268 Letter Count = 1149 Character Count = 1440
Do the following
To detect is a character ch is a letter use the following if statement if ch >= ‘A’ and ch <= ‘Z’ or ch >= ‘a’ and ch <= ‘z’:
You may want to embed this “if statement” in a Boolean valued function (isLetter(ch)) that returns True if ch is a letter; otherwise it returns False
Ask the user for the file name.
For this assignment, the easiest way do the following three counts is to first input the text file as one very long string using the read() method. Don’t use readline() or readlines()here, unless using the read() method crashes the program.
The character count is simply the length of the string you input from Step 3 above. Use the len() function
1
To determine the letter count you will need to go through the string character by character using a for loop testing if each character is a letter or not. If it is a letter, increment a letter count variable.
Checking for the number of words is easily done using the split method for strings (see page 136 for details). Recall that string.split( ) was used to break up a date such as “10/25/2016” into a list of three strings. That is "10/25/20165".split("/") returns the list ["10", "25", "2016"].
By default if no “split” character is given, the string will be split wherever a space occurs. This makes it perfect to split a text string into a list of individual words which then can be counted by using the len() to get the length of the list.
Output your three counts using string formatting to properly line up your three counts and close the file
Short Summary:
Implemented the program as per requirement
Attached source code and sample output
(Updated the code with if block as mentioned in the question)
**************Please do upvote to appreciate our time.
Thank you!******************
Source Code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Scanner;
/** This class reads input sentence from a file and prints the
no of words, characters and letters**/
public class StringOpsMain {
public static void main(String[] args) {
try {
BufferedReader
bufferedReader = new BufferedReader(new
FileReader("C:\\Users\\asus\\Input.txt"));
calculateAndPrintChars(bufferedReader.readLine());
bufferedReader.close();
} catch (IOException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
//This method prints the no of characters,letters and words in
the given input string
static void calculateAndPrintChars(String
sentence)
{
//Form a string array
String[]
sentenceArray=sentence.split(" ");
System.out.println("Word Count =
"+sentenceArray.length);
int letterCount=0;
//Loop to find if its a
letter
for(int
i=0;i<sentence.length();i++)
{
if(isLetter(sentence.charAt(i)))
letterCount++;
}
System.out.println("Letter Count =
"+sentence.length());
System.out.println("Character Count
= "+letterCount);
}
static boolean isLetter(char ch)
{
if ((ch >= 'A' && ch
<= 'Z') || (ch >= 'a' && ch <= 'z'))
return
true;
return false;
}
}
Code Screenshot:
Output:
**************Please do upvote to appreciate our time.
Thank you!******************