Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1 Your program should take a string representing a sentence in English and

ID: 3772504 • Letter: P

Question

Part 1

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:

Part 2

Create a method that does the same thing as your program, but takes its input from, and provides its output to, a calling program (do not use the IO module to interact with a human user). Define appropriate arguments (inputs) and return type, and implement this method. Test your method by calling it from another program.

Explanation / Answer

import java.util.*;

import java.io.*;

public class ProperPunctuationIMP

{

public static void properPunctuationPart1(String inString1)

{      

//remove spaces from beginning& end of the string

inString1 = inString1.trim();

          StringBuilder newStringB = new StringBuilder(inString1);

          for(int k1=0; k1<newStringB.length(); k1++)

{

//capitalize the first letter

              if(k1==0 && newStringB.charAt(k1)>='a' && newStringB.charAt(k1)<='z')

                   newStringB.setCharAt(k1, ((char)(newStringB.charAt(k1) - 32)));

//convert to lowercase letter if any uppercase letter other than first letter

              else if(newStringB.charAt(k1)>='A' && newStringB.charAt(k1)<='Z')

            newStringB.setCharAt(k1,((char)(newStringB.charAt(k1) + 32)));

     //remove extra spaces between words

              else if(newStringB.charAt(k1)== ' ')

{

        if(k1!= newStringB.length()-1)

                        while(newStringB.charAt(k1+1)==' ')

                             newStringB.deleteCharAt(k1+1);

              }

              else if(newStringB.charAt(k1)=='i')

{

                   if(newStringB.charAt(k1-1)==' ' && newStringB.charAt(k1+1) == ' ')

                        newStringB.setCharAt(k1, ((char)(newStringB.charAt(k1) - 32)));

              }         

              if(k1==newStringB.length()-1 && newStringB.charAt(k1) != '.')

                   newStringB.append('.');        

          }

//PRINT THE STRING

          System.out.println("Result:" +newStringB.toString());

   }

//RETURN THE FORMATTED STRING

public static String properPunctuationPart2(String inString1)

{

          inString1 = inString1.trim();

          StringBuilder newStringB = new StringBuilder(inString1);

          for(int k1=0; k1<newStringB.length(); k1++)

{

              if(k1==0 && newStringB.charAt(k1)>='a' && newStringB.charAt(k1)<='z')

                   newStringB.setCharAt(k1, ((char)(newStringB.charAt(k1) - 32)));

              else if(newStringB.charAt(k1)>='A' && newStringB.charAt(k1)<='Z')

            newStringB.setCharAt(k1,((char)(newStringB.charAt(k1) + 32)));

    

              else if(newStringB.charAt(k1)== ' ')

{

        if(k1!= newStringB.length()-1)

                        while(newStringB.charAt(k1+1)==' ')

                             newStringB.deleteCharAt(k1+1);

              }

              else if(newStringB.charAt(k1)=='i')

{

                   if(newStringB.charAt(k1-1)==' ' && newStringB.charAt(k1+1) == ' ')

                        newStringB.setCharAt(k1, ((char)(newStringB.charAt(k1) - 32)));

              }         

              if(k1==newStringB.length()-1 && newStringB.charAt(k1) != '.')

                   newStringB.append('.');

          }

          return newStringB.toString();

   }

//MAIN METHOD

public static void main(String[] args)

{

String inString1="corRect pUNCtuation    is hard, i tHINk";

properPunctuationPart1(inString1);

System.out.println(properPunctuationPart2 (inString1));

}

}