Question

In: Computer Science

Your program should take a string representing a sentence in English and format it properly. The...

Your program should take a string representing a sentence in English and format it properly. The input sentence may have any or all of the following errors:

Random letters may be capitalized.

The sentence may not end with a proper punctuation mark (period, question mark, or exclamation point).

There may be spaces at the beginning or end, or more than one space between words.

Format the sentence to fit the following rules:

The first letter of the first word should be capitalized.

The word "I" should be capitalized.

All other letters in all words should be in lowercase.

The sentence should end with a punctuation mark. If the original sentence did not end with a punctuation mark, add a period. (If the original sentence ended with more than one punctuation mark, it is not your responsibility to detect or fix this. However, you should not cause this problem yourself by adding a period if the sentence already ends with a punctuation mark.)

There should be no spaces at the beginning or end of the sentence.

There should be only a single space between words.

Example:

java Sentence

  corRect pUNCtuation    is hard, i  tHINk

RESULT: Correct punctuation is hard, I think.

Solutions

Expert Solution

import java.util.Arrays;

import java.util.Scanner;

public class SentenceChecker {

   public static void main(String[] args) {

       boolean punctuationCheck = false;

       boolean firstCharecterCheck = false;

       Scanner input = new Scanner(System.in);

       System.out.println("Enter The Sentence");

       String sentence = input.nextLine();

       String[] words = sentence.split("\\s+");

       words[0] = words[0].trim();

       String lastWord = words[words.length - 1];

       lastWord = lastWord.trim();

       Character lastChar = lastWord.charAt(lastWord.length() - 1);

       if (lastChar != '.' && lastChar != '?' && lastChar != '!') {

           lastChar = '.';

           punctuationCheck = true;

       }

       if (punctuationCheck)

           lastWord = lastWord + lastChar;

       StringBuffer correctedSententce = new StringBuffer();

       String firstWord = words[0];

       Character firstChar = firstWord.charAt(0);

       if (!Character.isUpperCase(firstChar)) {

           firstWord = Character.toUpperCase(firstChar) + firstWord.toLowerCase().substring(1, firstWord.length());

       }

       correctedSententce.append(firstWord);

       for (int i = 1; i < words.length - 1; i++) {

           String string = words[i];

           if (string.equals("i")) {

               string = "I";

               correctedSententce.append(" " + string);

               continue;

           }

           firstChar = string.charAt(0);

           if (Character.isUpperCase(firstChar)) {

               firstCharecterCheck = true;

           }

           if (firstCharecterCheck) {

               string = string.toLowerCase();

               string = firstChar + string.substring(1, string.length());

           } else {

               string = string.toLowerCase();

           }

           correctedSententce.append(" " + string);

       }

       lastWord = lastWord.toLowerCase();

       correctedSententce.append(" " + lastWord);

       System.out.println(correctedSententce);

   }

}


Related Solutions

Your assignment is to build a program that can take a string as input and produce...
Your assignment is to build a program that can take a string as input and produce a “frequency list” of all of the words in the string (see the definition of a word below.) For the purposes of this assignment, the input strings can be assumed not to contain escape characters (\n, \t, …) and to be readable with a single input() statement. When your program ends, it prints the list of words. In the output, each line contains of...
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
Write a program to take input of scores for Chemistry, Biology and English for a student...
Write a program to take input of scores for Chemistry, Biology and English for a student and display the average with 2 decimal places if all of the three scores are in the range between 1 and 100 inclusive. Program should also display the course name for which scores are entered out of range telling Average couldn't be calculated. Following is a sample run: Enter scores for Chemistry, Biology and English between 1 and 100: 45 89 96 Avg score...
PLEASE TYPE!! EXPLAIN PROPERLY WITH ATLEAST 200 WORDS OR MORE NOT JUST ONE SENTENCE IT SHOULD...
PLEASE TYPE!! EXPLAIN PROPERLY WITH ATLEAST 200 WORDS OR MORE NOT JUST ONE SENTENCE IT SHOULD BE ATLEAST ONE OR TWO PARAGRAPHS WITH A GOOD EXPLANATION BECAUSE YOURE AN EXPERT RIGHT SO IT SHOULDNT BE THAT HARD FOR YOU AND I WANT MY $3 TO BE WORTH IT WHEN YOU GIVE AN ANSWER ALSO DONT JUST COPY AND PASTE BECAUSE I WILL BE CHECKING PLAGIARISM! Calculate your sleep debt for the week (show your calculations). Would you say this week...
The assignment is to build a program in Python that can take a string as input...
The assignment is to build a program in Python that can take a string as input and produce a “frequency list” of all of the wordsin the string (see the definition of a word below.)  For the purposes of this assignment, the input strings can be assumed not to contain escape characters (\n, \t, …) and to be readable with a single input() statement. When your program ends, it prints the list of words.  In the output, each line contains of a...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input a description of several process schedules (i.e., lists of send, receive or print operations). The output of your program will be a linearization of these events in the order actually performed, annotated with Lamport clock values. The input of the program will be a collection of processes, each with a list of operations to perform. The processes are named p1...pn for some n (you...
Write a program to remove extra blanks from text files. Your program should replace any string...
Write a program to remove extra blanks from text files. Your program should replace any string of two or more blanks with one single blank. It should work as follows: * Create a temporary file. * Copy from the input file to the temporary file, but do not copy extra blanks. * Copy the contents of the temporary file back into the original file. * Remove the temporary file. Your temporary file must have a different name than any existing...
Create a program that translates English into Pig Latin. The function will take the first letter...
Create a program that translates English into Pig Latin. The function will take the first letter of each word in the sentence only if it’s a not a vowel, and place it at the end of the word followed by “ay”. Your program must be case ​ins​ ensitive. You must write the functiontranslate() ​that takes in a single English word and ​returns​ its Pig Latin translation. Remembertranslatemusttake​onlyonewordasinput.​ ############################################################# # translate() takes a single english word translates it to #pig latin...
maximumST is a function that should take an n × n vector (representing the adjacency matrix...
maximumST is a function that should take an n × n vector (representing the adjacency matrix of a graph) as input and return the value of the maximum spanning tree of the graph defined by the n × n vector. Code: #include <vector> #include <cmath> #include <iostream> #include <cstdio> using namespace std; double maximumST( vector< vector<double> > adjacencyMatrix ){   vector<int> row(4,-1);   vector< vector<int> > matrix(5,row);   cerr << matrix.size() << endl;   cerr << matrix[0].size() << endl;   for( int i = 0;...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a whole paragraph with punctuations (up to 500 letters) either input from user, initialize or read from file and provide following functionalities within a class: a)   Declare class Paragraph_Analysis b)   Member Function: SearchWord (to search for a particular word) c)   Member Function: SearchLetter (to search for a particular letter) d)   Member Function: WordCount (to count total words) e)   Member Function: LetterCount (ONLY to count all...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT