Explore Westonci.ca, the leading Q&A site where experts provide accurate and helpful answers to all your questions. Discover in-depth answers to your questions from a wide network of professionals on our user-friendly Q&A platform. Discover detailed answers to your questions from a wide network of experts on our comprehensive Q&A platform.

Write a program that takes a String containing a text using the method signature

String useProperGrammar(String text)
Your method should replace the word ‘2’ with ‘to’ and return the updated text.

For example,

useProperGrammar("can you go 2 the store?")
should return

"can you go to the store?"
This method should also print out the number of grammatical errors that were fixed.

For example, for useProperGrammar("back 2 back 2 back"), the method would also print:

Fixed 2 grammatical errors:
In the main method, ask the user to input a String, and print the results of useProperGrammar using the user input.


Sagot :

import java.util.*;

public class MyClass {

   public static String useProperGrammar(String message){

       String newMessage = "";

       int count = 0;

       for (int i = 0; i < message.length(); i++){

           if(message.charAt(i) == '2'){

               newMessage = newMessage + "to";

               count ++;

           

           }

           else{

               newMessage = newMessage + message.charAt(i);

           }

       

       }

       System.out.println("Fixed "+count+" grammatical error(s):");

       return newMessage;

   }

   public static void main(String args[]) {

     Scanner scan = new Scanner(System.in);

     System.out.println("Enter a string: ");

     String text = scan.nextLine();

     System.out.println(useProperGrammar(text));

   }

}

I hope this helps.