answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Mama L [17]
1 year ago
10

Exercise 3: Function Write a function named word_count that accepts a string as its parameter and returns the number of words in

the string. A word is a sequence of one or more non-space characters (any character other than ' '). For example, the call of word_count("hello, how are you?") should return 4. Constraints: • Do not use any data structures such as arrays to help you solve this problem. • Do not use the String function explode on this problem. • But you can declare as many simple variables like Integer, String, etc. as you like. Keep it simple.

Computers and Technology
1 answer:
Kryger [21]1 year ago
8 0

Answer:

I am writing a PHP program.

<?php  

function word_count($string){   // function that takes a string a parameter and returns the number of words in that string

  $space = 0;   // this variable is used in order to detect space, new line and tab spaces in a string

  $words = 1; // this variable is used to identify word presence in string

  $include = $space;  //used to maintain the state of $words and $space

  $counter = 0; // this counts the number of words in the string

  $i = 0; //this moves through each character of the string

  while ($i < strlen($string)){ // iterates through every character of string until the last character of string is reached

      if ($string[$i] == " "||$string[$i] == "\n" || $string[$i] == "\t")  //if the space or new line or tab space is identified in the string

          $include = $space; //set the state of include as space if the next character is a space newline or a tab space

      else if ($include == $space) {  //if next character is a word and current state i.e. $include holds $space

          $include = $words;   //  then set the state i.e. $include as $words

          ++$counter; } //increments i to move to next character at each iteration

      ++$i;  } //returns the number of words in a string

  return $counter; }

$str = "Hello, how are you ";  //sample string

echo "Words: " . word_count($str);  // calls word_count function to return number of words in str.

?>  

Explanation:

The program has a function word_count that accepts a string as its parameter and returns the number of words in the string. In the function there are three variables $space and $words and $include used as state variables. For instance $space identifies space, new line and tab spaces in the $string so it specifies that a space has occurred. $words identifies words in the $string so it specifies that a word has occurred. $include holds this state information. $i moves through every character of the $string and when a space occurs in the $string then the state $include is set to $space. When a word occurs then state $include is set to $words. $counter variable, which is used to count the number of words is incremented to 1 when previous state is $space and next character is a word character. Every time a word is seen in the string, this variable is incremented to 1 to keep track of number of words in the string. When $i reaches the end of the string then this loop stops and counter returns the number of words in the $string.

You might be interested in
In which of the following situations may you pass on the right
Natasha2012 [34]
In driving your vehicle, it is best to pass on the right after you slow your vehicle down and check for the road whether there is an upcoming vehicle or not. When the road is clear, it is best and safe to pass on the right after making sure that the coast is clear.
3 0
1 year ago
Read 2 more answers
Eric is working on a computer that has a device driver error. Eric can find the name of the device driver however the actual dev
guajiro [1.7K]

Answer:

Windows\System32\drivers folder

Explanation:

6 0
1 year ago
Read 2 more answers
What data unit is encapsulated inside a packet?<br> frame<br> datagram<br> segment<br> session
AVprozaik [17]
The answer is frame
8 0
2 years ago
Read 2 more answers
A student who used a regression model that included indicator variables was upset when receiving only the following output on th
jeka57 [31]

Answer:

This is a multicolinearity problem and the student should determine the variable(s) that cause(s) the problem and remove it.

Explanation:

This information means that there exists a linear combination between the independent variables. The problem might have developed due to multicolinearity producing almost perfectly linearly dependent columns.

This could also be as a results of single matrix created when the student use an incorrect indicator variables and included an additional indicator column which created linearly dependent columns.

4 0
1 year ago
Write a method called makeStars. The method receives an int parameter that is guaranteed not to be negative. The method returns
serious [3.7K]

Answer:

// import the Scanner class

// to allow the program receive user input

import java.util.Scanner;

// The class Solution is defined

public class Solution{

   // main method to begin program execution

   public static void main(String[] args) {

       // scanner object scan is declared

       Scanner scan = new Scanner(System.in);

       // Prompt the user to enter a number

       System.out.println("Enter the number of stars you want: ");

       // user input is assigned to numOfStar

       int numOfStar = scan.nextInt();

       // call the makeStars method

       makeStars(numOfStar);

   }

   

   // makeStars method print stars using recursion

   // the method call itself till starNum == 0

   public static void makeStars(int starNum){

       if (starNum != 0){

           System.out.print("*");

           makeStars(starNum -1);

       }

   }

}

Explanation:

The code is well comment and solve the problem using recursion.

6 0
2 years ago
Other questions:
  • Which port, along with a special card and cables, supports the connection of microcomputers, modems, and printers in a local are
    6·1 answer
  • Suppose you develop an app and want to generate revenue but do not want to maintain the app. What would be your best choice?
    9·1 answer
  • Mr. Cooper would like to customize his Excel software so his students can create an electronic graph in Excel for their lab repo
    6·1 answer
  • A(n) ____ attack is when a system is compromised and used to attack other systems. a. indirect b. direct c. object d. subject
    13·1 answer
  • Write the definition of a function named averager that receives a double parameter and return-- as a double -- the average value
    12·1 answer
  • Sites like Zillow get input about house prices from a database and provide nice summaries for readers. Write a program with two
    12·1 answer
  • Complete the program below that takes in one positive, odd, integer, n (at least 3), and prints a "diamond" shape of stars with
    9·1 answer
  • ___________is used for drawing 3D objects in the field of Science and Engineering.
    12·2 answers
  • Through which of the devices listed are we able to connect to wireless networks? Check all that apply.
    14·1 answer
  • Chinh wants to have a program print, "Sorry, but that isn’t one of your options" until the user enters the correct information.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!