khameleonjr10
24.07.2020 •
Computers and Technology
php 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.
Solved
Show answers
More tips
- H Health and Medicine How to Calculate Pregnancy Due Date?...
- C Computers and Internet Step-by-Step Guide on How to Download Music to Your iPhone...
- A Animals and plants Unraveling the Mystery of Loch Ness: What Does the Loch Ness Monster Look Like?...
- L Leisure and Entertainment Should You Buy a Ceramic Knife?...
- C Computers and Internet How to easily and quickly disable Firebug in Gmail and Google Docs...
- G Goods and services How to sew a ribbon: Tips for beginners...
- F Food and Cooking How to Make Mayonnaise at Home? Secrets of Homemade Mayonnaise...
- C Computers and Internet Which Phone is Best for Internet Surfing?...
- F Food and Cooking Everything You Need to Know About Pasta...
- C Computers and Internet How to Choose a Monitor?...
Answers on questions: Computers and Technology
- C Computers and Technology The abuse of children is a symptom of...
- C Computers and Technology There are two assembly lines, Line #1 and Line #2, that produce the same products. After examining one conformance quality of products made by each assembly line,...
- C Computers and Technology When preparing the heading for an MLA Format Academic Report, which of the following shows the proper order of the for lines of the header? A. Student Name, Instructor,...
- E English Which detail from the odyssey best shows that odysseus can be vain...
- M Mathematics Factor 60 – 36w to identify the equivalent expressions....
- M Mathematics Phil and his family want to do something fun and memorable on leave date age come up with an activity attending a trapeze class going fishing going on a hike or...
- E English Question 5 (1 point) saved choose the type of supporting detail that doesn t belong. thesis statement hypothetical examples research materials authoritative evidence...
- M Mathematics Tony works at a gas station. he serves 12 cars in 20 min. at this rate, how many cars can he serve in 25 min?...
- E English Which is the best summary of the passage s theme...
- M Mathematics Ireally need in geometry can someone me i waited like 2 days...
Ответ:
<?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.
Ответ: