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

In java only please It is a well-known phenomenon that most people are easily ab

ID: 3572036 • Letter: I

Question

In java only please It is a well-known phenomenon that most people are easily able to read a text whose words have two characters flipped, provided the first and last letter of each word are not changed. For example, I dn'ot gvie a dman for a man taht can olny sepll a wrod one way. (Mark Twain) Write a program with a method scramble(String word) that reads the contents of a file with a paragraph in it and constructs a scrambled version of the paragraph, randomly flipping two characters other than the first and last for all words greater than length 3.

Explanation / Answer

import java.util.*;
import java.io.*;

public class ScrambleWord
{
public static String scramble(String s)
{
String ss="";
int i=1;
  
// check if word's length is less than 5 then change 2nd and third alphabets else change 4th and 5th
char arr[] = s.toCharArray();
if(arr.length > 5) i=3;
char c = arr[i];
arr[i]=arr[i+1];
arr[i+1]=c;
return new String (arr);
}
public static void main(String s[]) throws Exception
{
// to read file
Scanner sc = new Scanner(new File("paragraph.txt"));
// to write to file
BufferedWriter fw = new BufferedWriter(new FileWriter("scrambled.txt", false));
String word, line;
String words[];   

  
while(sc.hasNextLine())
{
// read each line
line=sc.nextLine();
// break line into words array
words = line.split("\s");

for(int i=0;i<words.length;i++)
{
word=words[i];
  
// if word length is greater than threee then scrample it
if(word.length() >3)
{
word = scramble(word);
}
  
// write word to file
fw.write(word+" ");
System.out.print(words[i]+" ");
}
fw.newLine();
System.out.println(" ");
}
fw.close();

}


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote