Question: Encode a string by replacing all letters i with ! and all letters s with $. Use the replace method. Do this with the variable

Encode a string by replacing all letters "i" with "!" and all letters "s" with "$". Use the replace method. Do this with the variable town, assigning the result to variable own encoding, and with the variable sentence, assigning the result to variable sentence encoding.
*/

public class ReplaceTester
{
public static void main(String[] args)
{
String town = "Mississauga" ;
String townEncoding ;
String sentence = "This is a simple sentence." ;
      String sentence encoding ;
      //-----------Start below here. To do: approximate lines of code = 4
      // 1. Assign to townEncoding the result of applying the replace method to town to replace "i" with "!" ;
     
      //2. now replace s to $ in string townEncoding ;
     
      //3. assign to sentence encoding the result of applying to replace() on sentence to replace "i" with "!".  
     
      //4. now replace s with $ in string sentence encoding.
     
      //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
      System.out.println("Encoding of town:" + townEncoding) ;
      System.out.println("Encoding of sentence:" + sentenceEncoding) ;
System.out.println("Expect:") ;
System.out.println("Encoding of town: M!$$!$$auga" ) ;
      System.out.println("Encoding of sentence: Th!$ !$ a $!mple $entence.") ;
}
}

Step by Step Solution

3.38 Rating (160 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public class ReplaceTester public static void mainString args String town Mississauga String townEnc... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!