In: Computer Science
Prompt the user to enter a string of their choosing. Store the
text in a string. Output the string. (1 pt)
Ex:
Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue!
(2) Implement a print_menu() function, which has a string as a
parameter, outputs a menu of user options for analyzing/editing the
string, and returns the user's entered menu option and the sample
text string (which can be edited inside the print_menu() function).
Each option is represented by a single character.
If an invalid character is entered, continue to prompt for a
valid choice. Hint: Implement the Quit menu option before
implementing other options. Call print_menu() in the main
section of your code. Continue to call print_menu() until the user
enters q to Quit. (3 pts)
Ex:
MENU c - Number of non-whitespace characters w - Number of words f - Fix capitalization r - Replace punctuation s - Shorten spaces q - Quit Choose an option:
(3) Implement the get_num_of_non_WS_characters() function.
get_num_of_non_WS_characters() has a string parameter and returns
the number of characters in the string, excluding all whitespace.
Call get_num_of_non_WS_characters() in the print_menu() function.
(4 pts)
Ex:
Number of non-whitespace characters: 181
(4) Implement the get_num_of_words() function. get_num_of_words()
has a string parameter and returns the number of words in the
string. Hint: Words end when a space is reached except for the
last word in a sentence. Call get_num_of_words() in the
print_menu() function. (3 pts)
Ex:
Number of words: 35
(5) Implement the fix_capitalization() function.
fix_capitalization() has a string parameter and returns an updated
string, where lowercase letters at the beginning of sentences are
replaced with uppercase letters. fix_capitalization() also returns
the number of letters that have been capitalized. Call
fix_capitalization() in the print_menu() function, and then output
the the edited string followed by the number of letters
capitalized. Hint 1: Look up and use Python functions
.islower() and .upper() to complete this task. Hint 2: Create an
empty string and use string concatenation to make edits to the
string. (3 pts)
Ex:
Number of letters capitalized: 3 Edited text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!
(6) Implement the replace_punctuation() function.
replace_punctuation() has a string parameter and two keyword
argument parameters exclamation_count and
semicolon_count. replace_punctuation() updates the
string by replacing each exclamation point (!) character with a
period (.) and each semicolon (;) character with a comma (,).
replace_punctuation() also counts the number of times each
character is replaced and outputs those counts. Lastly,
replace_punctuation() returns the updated string. Call
replace_punctuation() in the print_menu() function, and then output
the edited string. (3 pts)
Ex:
Punctuation replaced exclamation_count: 1 semicolon_count: 2 Edited text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here, our hopes and our journeys continue.
(7) Implement the shorten_space() function. shorten_space() has a
string parameter and updates the string by replacing all sequences
of 2 or more spaces with a single space. shorten_space() returns
the string. Call shorten_space() in the print_menu() function, and
then output the edited string. Hint: Look up and use Python
function .isspace(). (3 pt)
Ex:
Edited text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue!
import java.util.Scanner;
public class Main {
static public void printMenu()
{
System.out.println();
System.out.println("MENU");
System.out.println("c - Number of non-whitespace characters");
System.out.println("w - Number of words");
System.out.println("f - Find text");
System.out.println("r - Replace all !'s");
System.out.println("s - Shorten spaces");
System.out.println("q - Quit");
System.out.println();
System.out.println("Choose an option: ");
return;
}
public static int getNumOfNonWSCharacters(String usrStr)
{
int i = 0;
int j = 0;
for (i = 0 ; i < usrStr.length(); ++i )
{
if (Character.isWhitespace(usrStr.charAt(i)) == false)
{
j = j + 1;
}
}
return (j);
}
public static int getNumOfWords(String userString)
{
int i = 0;
int wordCount = 0;
for (i = 0 ; i < userString.length(); ++i )
{
if (Character.isWhitespace(userString.charAt(i)) && Character.isWhitespace(userString.charAt(i -1)) == false)
{
++wordCount;
}
}
if (Character.isWhitespace(userString.length()) == false)
{
++wordCount;
}
return (wordCount);
}
public static int findText(String userWord, String userString)
{
int i = 0;
int j = 0;
int wordCount = 0;
String nextSegment = userString;
int userWordLength = userWord.length();
int userStringLength = userString.length();
while (userWordLength <= nextSegment.length() && nextSegment.indexOf(userWord) != -1)
{ //System.out.println(nextSegment.indexOf(userWord));
if (nextSegment.indexOf(userWord) != -1)
{//System.out.println(nextSegment);
j = nextSegment.indexOf(userWord);
++wordCount;
nextSegment = nextSegment.substring((j + userWordLength), nextSegment.length());
}
}
return (wordCount);
}
public static String replaceExclamation(String userString)
{
userString = userString.replace('!','.');
return userString;
}
public static String shortenSpace(String userInput)
{
int i = 0;
int j = 0;
String partialStr = "";
for (i = 1 ; i < userInput.length(); ++i )
{//System.out.println(partialStr);
if (Character.isWhitespace(userInput.charAt(i)) && Character.isWhitespace(userInput.charAt(i - 1)))
{ //System.out.println(i);
partialStr = partialStr.concat(userInput.substring(j,i));
j = i + 1;
}
}
if (Character.isWhitespace(userInput.length()) == false)
{
partialStr = partialStr.concat(userInput.substring(j,i));
userInput = partialStr;
}
return (userInput);
}
public static void main(String[] args) {
Scanner assistantEditor = new Scanner(System.in);
String userText;
String userFindWord;
char userCommand;
boolean quitCommand = false;
System.out.println("Enter a sample text: ");
userText = assistantEditor.nextLine();
System.out.println("You entered: " + userText);
Main.printMenu();
userCommand = assistantEditor.next().charAt(0);
while (quitCommand == false)
{
if (userCommand == 'c' || userCommand == 'w' || userCommand == 'f' || userCommand == 'r' || userCommand == 's' || userCommand == 'q' )
{
switch (userCommand)
{
case 'c':
System.out.println("Number of non-whitespace characters: " + Main.getNumOfNonWSCharacters(userText));
//quitCommand = true;
break;
case 'w':
System.out.println("Number of words: " + Main.getNumOfWords(userText));
//quitCommand = true;
break;
case 'f':
System.out.println("Enter a word or phrase to be found: ");
assistantEditor.nextLine();
userFindWord = assistantEditor.nextLine();
System.out.println("\"" + userFindWord + "\" instances: " + Main.findText(userFindWord, userText));
//quitCommand = true;
break;
case 'r':
System.out.println("Edited text: " + Main.replaceExclamation(userText));
//quitCommand = true;
break;
case 's':
System.out.println("Edited text: " + Main.shortenSpace(userText));
//quitCommand = true;
break;
case 'q':
quitCommand = true;
break;
default :
System.out.print("boo");
break;
}
userCommand = 't';
}
else
{
Main.printMenu();
userCommand = assistantEditor.next().charAt(0);
}
}
return;
}
}
This is the Java program required and the output is'
Hope this is helpful
Kindly please please upvote as it helps me alot
please upvote