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
belka [17]
3 years ago
5

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

s 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:
Bingel [31]3 years ago
5 0

Answer:

<?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
You need to design a backup strategy. You need to ensure that all servers are backed up every Friday night and a complete copy o
cestrela7 [59]

Answer: (A) full backup

Explanation: A backup strategy is a term used to describe the various effective approach engaged in creating a backup for data or information.

A FULL BACK UP IS THE VARIOUS APPROACH AND SYSTEMS PUT IN PLACE BY A COMPUTER USER OR AN INFORMATION TECHNOLOGY EXPERT IN ORDER TO CREATE AN ADDITIONAL SAMPLE OR COPY OF AN INFORMATION OR A DATA IN S SINGLE BACKUP.

8 0
3 years ago
Technician A says that the excessive length of a heater hose is intended to protect the heater core from undue stress. Technicia
hammer [34]
It is both true that <span>Technician A says that the excessive length of a heater hose is intended to protect the heater core from undue stress. Technician B says that excessive wear adds to the length of a heater hose, and a replacement heater hose should be roughly three to four inches shorter than its predecessor.  So the asnwer is letter B.</span>
6 0
3 years ago
Which of the following forces all traffic, communications, and activities through a single pathway or channel that can be used t
Ad libitum [116K]

Answer:

Option b. Choke point

Explanation:

In firewall, a choke point can be defined as a single point which allows all of the network traffic including incoming and outgoing to be funneled. As the passage of the whole traffic through the choke point regulates the flow as it focus monitoring  and controls bandwidth consumption, provides authentication, control efforts like Internet firewalls.

7 0
2 years ago
9. A change in the appearance of a value or label in a cell
alexandr402 [8]

Answer:

format

alignment

excel

=

Explanation: i'm an accountant

4 0
2 years ago
Hi!
baherus [9]

Answer:

Radius = 14 cm = 0.00014 km

Circumference = 2πr = 2 × 22/7 × 14/100000 = 0.00088 km

As it went thousand times , distance covered = 0.00088 × 1000 = 0.88 km

8 0
3 years ago
Other questions:
  • Make three statements about technology
    9·1 answer
  • ¿Ha existido en la historia de nuestra humanidad alguien que hubiera hecho posible el sueño del ser humano en obtener energía li
    11·1 answer
  • Your mom is trying to save room on her hard drive and wants to uninstall some of her applications. She asks you how to do this.
    9·2 answers
  • Study and compare the tables and draw conclusions.
    13·1 answer
  • Use the _______ command to combine the selected cells and center them. merge across center across selection merge cells merge an
    7·1 answer
  • Data governance consists of? A. the processes, methods, and techniques to ensure that data is of high quality, reliable, and uni
    11·1 answer
  • All linear programming problems have all of the following properties EXCEPT
    9·2 answers
  • If you have configured your prompt so that it does not show your working directory, you can use the ____ command to verify in wh
    15·1 answer
  • Complete the statement below using the correct term.<br>Website managers use<br>every day​
    11·2 answers
  • Super computer in nuclear energy ​
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!