In: Computer Science
(3) Implement the getNumOfNonWSCharacters() method.
getNumOfNonWSCharacters() has a string as a parameter and returns
the number of characters in the string, excluding all whitespace.
Call getNumOfNonWSCharacters() in the main() method. (4 pts)
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!
MENU
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit
Choose an option:
c
Number of non-whitespace characters: 181
Implement the getNumOfWords() method. getNumOfWords() has a string
as a 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 getNumOfWords() in the main() method.
(3 pts)
Ex:
Number of words: 35
ANSWER IN JAVA more coming
import java.util.Scanner;
public class JavaCode
{
   private static Scanner scanner=new Scanner(System.in);
   public static void main(String[] args)
   {
       String text;
       System.out.println("Enter a sample text: ");
       sampletext=scanner.nextLine();
       System.out.println("You entered: "+sampletext);
       char choice=printMenu();
       switch(choice)
       {
       case 'c':
           int countNonWhitespaces=getNumOfNonWSCharacters(text);
           System.out.println("Number of non-whitespace characters: "+countNonWhitespaces);
           break;
       case 'w':
           int wCount=getNumOfWords(text);
           System.out.println("Number of words: "+wCount);
           break;
       case 'f':
           System.out.println("Enter a word or phrase to be found: ");
           String findword=scanner.nextLine();
           int findCount=findText(text,findword);
           System.out.println("Number of words: "+findCount);
           break;
       case 'r':
           String newstring=replaceExclamation(text);
           System.out.println("Edited text:"+newstring);
           break;
       case 's':
           newstring=shortenSpace(text);
           System.out.println("Edited text:"+newstring);
           break;
       case 'q':
           System.exit(0);
       }
   }
   private static String shortenSpace(String text) {
       String temp = text.trim().replaceAll(" +", " ");
       return temp;
   }
   private static String replaceExclamation(String text) {
       String temp = text.replaceAll("!", "a");
       return temp;
   }
   private static int findText(String text, String find)
   {
       String[] words=text.split(" ");
       int count=0;
       for (int i = 0; i < words.length; i++)
       {
           if(words[i].equals(find))
               count++;
       }
       return count;
   }
   private static int getNumOfWords(String text) {
       String[] words=text.split(" ");
       return words.length;
   }
   private static int getNumOfNonWSCharacters(String text) {
       text=text.trim().replaceAll("\\s","");
       return text.length();          
   }
   private static char printMenu()
   {
       System.out.println("\nMENU");
       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("\nChoose an option: ");
       char choice=scanner.nextLine().charAt(0);
       return choice;
   }
}