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.
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
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...
I'm supposed to create a c++ program which is supposed to take a sentence as an...
I'm supposed to create a c++ program which is supposed to take a sentence as an input and then output a sorted list of the occurrence of each letter. ex. Enter a phrase: It's a hard knock life A2 I2 K2 C1 D1 E1 F1 H1 L1 N1 O1 R1 S1 T1 I was also given a recommended code to use as a bubble sort procedure bubbleSort( A : list of sortable items ) n = length(A) repeat swapped =...
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...
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;...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT