28.2k views
2 votes
(2) Complete the get_num_of_characters() function, which returns the number of characters in the user's string. We encourage you to use a for loop in this function. (2 pts) Extend the program by calling the get_num_of_characters() function and then output the returned result. (3) Extend the program further by implementing the string_without_whitespace() function. string_without_whitespace() returns the string's characters except for whitespace (spaces, tabs). Note: A tab is '\t'. (2 pt) Ex: Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have to fear is fear itself. Number of characters: 46 String with no whitespace: Theonlythingwehavetofearisfearitself. After you have completed and tested your lab, submit the code of the functions only to Zybook for autograding.

1 Answer

0 votes

Answer:

Code details below

Step-by-step explanation:

// Scanner is a class in the java.util package used to get the entry of primitive types like int, double etc. and also String.

import java.util.Scanner;

// The program must be able to open any text file specified by the user, and analyze the frequency of verbal ticks in the text.

public class TextAnalyzer {

// public : it is a access specifier that means it will be accessed by publically. static : it is access modifier that means when the java program is load then it will create the space in memory automatically. void : it is a return type i.e it does not return any value. main() : it is a method or a function name.

public static void main(String[] args) {

// the program will find the length of a string without using any loops and you may assume that the length of entered string is always less than any given number

Scanner scan = new Scanner(System.in);

System.out.println("Enter a sentence or phrase: ");

String s = scan.nextLine();

System.out.println("You entered: "+s);

int numOfCharacters = getNumOfCharacters(s);

System.out.println("");

System.out.println("Number of characters: "+numOfCharacters);

outputWithoutWhitespace(s);

}

public static int getNumOfCharacters(final String s){

int numOfCharCount = 0;

for(int i=0; i<s.length();i++){

numOfCharCount++;

}

return numOfCharCount;

}

// Using this function, we replace all whitespace with no space(“”)

public static void outputWithoutWhitespace(String s){

String str = "";

for(int i=0; i<s.length();i++){

char ch = s.charAt(i);

if(ch != ' ' && ch != '\t')

str = str + ch;

}

System.out.println("String with no whitespace: "+str);

}

}

User Nikhil Gupte
by
5.4k points