1answer.
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]
3 years 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]3 years 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
4. In Drag and Drop method of Excel , to copy the data , you need to press __________ key while dragging the cells.
alexgriva [62]

Answer:

The <u>control</u> [Ctrl] key

Explanation:

While holding down left click and pressing control key it will copy to where you drag and drop it.

6 0
3 years ago
Do you believe that OOP should be phased out and we should start working on some alternative(s)?
katen-ka-za [31]

Answer:

The answer to this question can be defined as below:

Explanation:

There are many new directions for solutions for any given problem in today's computer age, in which the "OOP's" is one of the philosophies of preference.  

  • If the issue for each of the several cases can be degraded in various classes, that reveal complex relationships.  
  • In this process the OO analysis is acceptable with other concepts, these issues usually produce difficult outcomes, that's why I don't believe in this modeling.
5 0
3 years ago
A computer’s BIOS is defined as the
Dennis_Churaev [7]
Basic input/output system
5 0
3 years ago
Read 2 more answers
In a non-price rationing system, consumers receive goods and services first-come, first served. Give me an example of a time whe
Anna35 [415]

When someone may be giving away something for free.

7 0
3 years ago
The area where a blast originates is referred to what perimeter/isolation zone
Anna [14]
The area where a blast originate from is referred to as PERIMETER. After a bomb blast incident, the first responders has to map out a safe pre-blast perimeter based on the requirements of Bureau of Alcohol, Tobacco, Firearms and Explosives [BATFE]. After this, the first inner perimeter, also called hot zone must be established. This is where the blast originated from and it is the point where victims rescue and treatment will start.
8 0
3 years ago
Other questions:
  • Which structure is sometimes called the "gateway to memory"?
    14·1 answer
  • Write a Python function called validateCreditCard that takes 8-digit credit card number as the input parameter (string value) an
    8·1 answer
  • (a) Write a SCHEME function (make-change n den) which outputs a list of change specifications. Namely, a call (make-change 11 (l
    8·1 answer
  • _____ is an effort by an employee to attract attention to a negligent, illegal, unethical, abusive, or dangerous act by a compan
    8·1 answer
  • Annalise received financial aid offers from two universities. Financial Analysis for Option A Costs per Year Financial Aid Packa
    8·2 answers
  • 40 points to answer ALL
    9·2 answers
  • The blank contains the most commonly used commands
    8·1 answer
  • You are supposed to do a report on something that interests you. You decided to do a report on dogs. As you research, you take n
    14·1 answer
  • How can blockchain be used to support sustainable business practices?
    8·1 answer
  • Write a function that receives a one-dimensional array of integers and returns a Python tuple with two values - the minimum and
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!