Question

In: Computer Science

using java language only write a code Have the function StringChallenge(str) take the str string parameter...

using java language only write a code

Have the function StringChallenge(str) take the str string parameter being passed and return the number of words the string contains (e.g. "Never eat shredded wheat or cake" would return 6). Words will be separated by single spaces.

Examples

Input: "Hello World"
Output: 2

Input: "one 22 three"
Output: 3

------- you have the following code edit it to get the result

mport java.util.*;

import java.io.*;

class Main {

  public static String StringChallenge(String str) {

    // code goes here  

    return str;

  }

  public static void main (String[] args) {  

    // keep this function call here     

    Scanner s = new Scanner(System.in);

    System.out.print(StringChallenge(s.nextLine()));

  }

}

Solutions

Expert Solution

Find the code for the above question below, read the comments provided in the code for better understanding. If found helpful please do upvote this.
Please refer to the screenshot of the code to understand the indentation of the code.

Copyable Code

import java.util.*; //to get unput form user

import java.io.*;

class Main {

    // this method returns the number of words in the str

    public static String StringChallenge(String str) {

        // first separate the string as per space

        String words[] = str.split(" ");

        // now store the strign size in str

        str = "" + words.length;

        return str;

    }

    //main driver code of the program

    public static void main(String[] args) {

        //Scanner class to get user input

        Scanner s = new Scanner(System.in);

        System.out.print(StringChallenge(s.nextLine()));

    }

}

Screenshot of code

Output


Related Solutions

Programming language is Java. Consider you have already created a validateString(String str) method that validates a...
Programming language is Java. Consider you have already created a validateString(String str) method that validates a String to ensure it contains exactly 4 characters. Create code that calls this method, sending user input as an argument (actual parameter). The user should be continuously prompted to enter a different string and informed of an error if the method returns false.
Write A function with an input parameter which is a string arithmetic statement which contains only...
Write A function with an input parameter which is a string arithmetic statement which contains only alphabet variables and binary operations including +, -, *, / and % and checks whether the statement is a valid arithmetic statement or not. If the statement is valid, the function returns true, otherwise returns false. • The statement might contain parenthesis as well. For instance: • a+b*a+c/c%y • (a+b)*(a/d-(a/b)) • You can make this assumption that the variable names contain only one alphabet...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
** in C language please Write a function that remove a substring of str beginning at...
** in C language please Write a function that remove a substring of str beginning at index idx and length len. // param str: the string being altered // param idx: the starting index of the removed chunk // param len: the number of characters to remove (length of sub> // removed) assumptions: // 0 <= idx // str is a valid string that terminates properly // // TODO: complete the function void remove_substr(char str[], int idx, int len) {
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
using java language "Data Structures" I have to write program which stores string entered by user...
using java language "Data Structures" I have to write program which stores string entered by user into cursor array implementation here is the code public static void main(String[] args) { CursorArray sorted =new CursorArray();//the strings must added here how can i store them                  String []inputs = new String[50];                   for (int i=0; i< 50; i++) {            System.out.println("Enter the words you want to sort and use exit to stop");...
Function 6: Sorting string function name (str) a. This function receives an string. Your task is...
Function 6: Sorting string function name (str) a. This function receives an string. Your task is loop through the each character of the string and separate all digists/letters/special characters. Display all digits at the beginning, followed by letters then the special chars and display result in console. For example if following string is passed to this function “ha1m2i3:n)” Result should be: 123hamin:)
C language only please and please make a simple code Write a function that will find...
C language only please and please make a simple code Write a function that will find whether there exist two integers that sum to the target integer. The function is to “return” three values.First, return “1” if the integers were found,return “-1” if your search was not successful.If you find two integers which add up to the target value, you should return their respective index position inside the array. Suggested prototype:int TwoSumFunction(int arr[], int size, int target, int*index1, int* index2);Inside...
#include <iostream> using namespace std; string* callMe(string*& s){ static string* str = new string{"Chiqita Banana"}; str...
#include <iostream> using namespace std; string* callMe(string*& s){ static string* str = new string{"Chiqita Banana"}; str = s; return str; } int main() { string* s = new string{"Hey Google"}; string* str = callMe(s);    cout << "Call me \'" + *str +"\'"; return 0; } 1. What is the output or the error of this program? Call me 'Hey Google' 2. And please explain the data flow in detail (to memory level)? Please help question 2 with explanation, thank...
Java language. 1. write a conversion method that have 2 parameter 1. feet and 2. inches(as...
Java language. 1. write a conversion method that have 2 parameter 1. feet and 2. inches(as small as 1/8 inch)(make sure precondition, can't be smaller than 1/8 inch) and give output as feet in decimal place. 2 Write a method to calculate the area of a rectangle that take in width and height (feet) (as small as 1/8 inch)(make sure precondition, can't be smaller than 1/8 inch) and round up to the closet feet. Write test cases for both method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT