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...
python program You are going to write a program that takes two inputs: A string representing...
python program You are going to write a program that takes two inputs: A string representing a list of names of the following form: first name 1, last name 1, (nick name 1); first name 2, last name 2, (nick name 2); ... A string representing part of or the complete family name. You are going to output all nick names for those names where there is a partial or complete match of the family name. If no match was...
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 Java program which takes a String representing an arithmetic expression as an input and...
Write a Java program which takes a String representing an arithmetic expression as an input and displays whether or not the expression is balanced. If the expression is not balanced (i.e. the wrong # of parentheses or in the wrong order) it will display an error message. For Example: Input an expression: ( ( 2 + 4 ) * 2 ) Expression is balanced Input an expression: ( 5 * 7 – 6 ) ) Error: Expression is not balanced.......
Write an assembly language program to input a string from the user. Your program should do...
Write an assembly language program to input a string from the user. Your program should do these two things: ***** I AM POSTING THIS QUESTION FOR THE THIRD TIME NOW,I WANT IT DONE USING INCLUDE IRVINE32.INC***** 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in:   "Hello thEre. How aRe yOu?" Your output should be:...
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...
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...
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...
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 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT