In: Computer Science
***USING JAVA Scenario: You will be writing a program that will allow a user to find and replace misspelled words anywhere in the phrase, for as many words as the user wishes. Once done (see example runs below), the program will print the final phrase and will show the Word Count, Character Count, the Longest Word and the Shortest Word.
Requirements:
Do not use Arrays for this assignment.
Do not use any String class methods (.phrase(), replace methods, regex methods) that parse the phrase for you. You will be writing the search and replace algorithm yourself
The blank spaces between words are not included when replacing a word. Your code should ignore them as it matches and replaces misspelled words.
For now, leave off any punctuation at the end of the phrase (or ignore it).
Make sure your code is neat and well structured / well-tabbed (this will help with debugging!), appropriately commented, with a comment header at the top.
This is a fantastic cumulative learning assignment. Write out pseudocode for this assignment and plan your implementation. This will help immensely!
Each occurrence of a misspelled word should be replaced, and I should be able to replace many different words through the use of the program (see below).
By “replaced” I mean that the final string that contains your phrase should contain the version with all misspellings desired by the user corrected.
If there are “longest” and “shortest” words that tie for character length, the most recent occurrence of either should “win.” For example: “Its either of or to but not both!” the shortest word would be “to”, the most recent shortest word towards the end of the phrase.
Due: Tuesday, September 24th by 11:59 p.m. on Canvas. (See Example Output on next page )
Short Run:
Please enter phrase to adjust: We aer teh Dukes of JMU
Enter word to be replaced: aer
Enter word to replace with: are
Current Phrase Version: "We are teh Dukes of JMU "
Continue Replacing Words? (y or n):y
Enter word to be replaced: teh
Enter word to replace with: the
Current Phrase Version: "We are the Dukes of JMU "
Continue Replacing Words? (y or n):n
Final Phrase:
============================
We are the Dukes of JMU
# of Words: 12
# of Characters: 24
Longest Word: "Dukes"
Shortest Word: "of"
============================
Longer Run:
Please enter phrase to adjust: When in teh course of humen events it becomes necessary for one poeple to dissolve the political bands that have connaected them with another
Enter word to be replaced:teh
Enter word to replace with:the
Current Phrase Version: "When in the course of humen events it becomes necessary for one poeple to dissolve the political bands that have connaected them with another "
Continue Replacing Words? (y or n):y
Enter word to be replaced:poeple
Enter word to replace with:people
Current Phrase Version: "When in the course of humen events it becomes necessary for one people to dissolve the political bands that have connaected them with another "
Continue Replacing Words? (y or n):y
Enter word to be replaced:connaected
Enter word to replace with:connected
Current Phrase Version: "When in the course of humen events it becomes necessary for one people to dissolve the political bands that have connected them with another "
Continue Replacing Words? (y or n):y
Enter word to be replaced:humen
Enter word to replace with:Human
Current Phrase Version: "When in the course of Human events it becomes necessary for one people to dissolve the political bands that have connected them with another "
Continue Replacing Words? (y or n):n
Final Phrase:
============================
When in the course of Human events it becomes necessary for one people to dissolve the political bands that have connected them with another
# of Words: 96
# of Characters: 141
Longest Word: "connected"
Shortest Word: "to"
============================
Program
import java.util.*;
class ReplaceWord
{
static String sentence;
//method to replace oldword with newword
static void replace(String oldword, String
newword)
{
int j;
for(int i=0; i<sentence.length()
- oldword.length(); i++)
{
if(oldword.charAt(0)==sentence.charAt(i))
{
for(j=0; j<oldword.length(); j++)
{
if(oldword.charAt(j)!=sentence.charAt(i+j)) break;
}
if(j==oldword.length())
sentence =
sentence.substring(0, i) + newword +
sentence.substring(i+newword.length());
}
}
}
//main method
public static void main (String[] args)
{
Scanner sc = new
Scanner(System.in);
char ch;
System.out.print("Please enter
phrase to adjust: ");
sentence = sc.nextLine();
do{
System.out.print("Enter word to be replaced: ");
String oldword =
sc.next();
System.out.print("Enter word to replace with: ");
String newword =
sc.next();
replace(oldword,
newword);
System.out.println("Current Phrase Version: " + sentence);
System.out.print("Continue Replacing Words? (y or n): ");
ch =
sc.next().charAt(0);
}while(ch=='y' ||ch=='Y');
int wc = 1;
int t = 0, k = 0;
String longest = "", word;
for(k=0; k<sentence.length(); k++)
{
if(sentence.charAt(k)==' ')
break;
}
String shortest = sentence.substring(0, k);
for(int i=0; i<sentence.length(); i++)
{
if(sentence.charAt(i)==' '
&& i<=sentence.length() &&
sentence.charAt(i+1)!=' ')
{
wc++;
word =
sentence.substring(t, i);
if(shortest.length() >= word.length())
shortest = word;
if(longest.length() <= word.length())
longest = word;
System.out.println(word);
t = i + 1;
}
}
System.out.println("Final Phrase:");
System.out.println("============================");
System.out.println(sentence);
System.out.println("# of Words: " + wc);
System.out.println("# of Characters: " +
sentence.length());
System.out.println("Longest Word: " + longest);
System.out.println("Shortest Word: " +
shortest);
}
}
Output:
Please enter phrase to adjust: We aer teh Dukes of JMU
Enter word to be replaced: aer
Enter word to replace with: are
Current Phrase Version: We are teh Dukes of JMU
Continue Replacing Words? (y or n): y
Enter word to be replaced: teh
Enter word to replace with: the
Current Phrase Version: We are the Dukes of JMU
Continue Replacing Words? (y or n): n
We
are
the
Dukes
of
Final Phrase:
============================
We are the Dukes of JMU
# of Words: 6
# of Characters: 24
Longest Word: Dukes
Shortest Word: of
Process completed.