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
The overall appearance of goods and services can be trademarked.<br> True<br> False
aliya0001 [1]

Answer:

true

Explanation:

Will you mark me the brainest plz I will follow you

7 0
3 years ago
Read 2 more answers
Given that slotted ALOHA contains three active stations: X, Y, Z. The probability of generating frames by these stations in a ti
Stella [2.4K]

Answer:

Throughput of X, Y, Z will be 0.081

The network throughput will be 0.243

Explanation:

Throughput of X will be Tx = Px*(1-Py)*(1-Pz)=0.1*0.9*0.9=0.081

Throughput for Y and Z can be calculated in same way.

Throughput of network = Tx+Ty+Tz=0.081+0.081+0.081=0.243

3 0
3 years ago
PLS ANSWER ASAP I WILL GIVE BRAINLIEST
vladimir1956 [14]

Answer: 'Scope' Is <u>NOT</u> a basic programming sturtcure

Explanation: Surprisingly, it can often be broken down into <u>three simple programming structures called sequences, selections, and loops</u>. ... These come together to form the most basic instructions and algorithms for all types of software.

5 0
3 years ago
When the result of an expression is assigned to a temporary memory location, what is the size of memory allocated?
MAXImum [283]

Answer:

Default size is allocated dynamically

Explanation:

Dynamic memory allocation refers to managing system memory at runtime.

Dynamic memory allocation uses the heap space of the system memory.

When the result of an expression in a code, such as C++, is assigned to a computer's temporary memory location, the default size of the memory is used at runtime by this dynamic memory allocation.

4 0
3 years ago
the advertisement below is an example of a(n) the advertisement below is an example of a(n) informative advertising objective pe
vlabodo [156]

The advertisement below is an example of an <u>informative advertising</u>. Thus, option A is the correct option.

<h3>What is informative advertising?</h3>

Utilizing facts to highlight the advantages of a product's features rather than pandering to consumers' emotions is used in informative advertising.

Any advertisement has one main objective: to sell products. The choice of methods represents the only distinction. To achieve a particular objective, each of them offers unique opportunities. Additionally, different industries seem to benefit from particular techniques more than others.

Through the use of data and statistics that demonstrate the worth and applicability of a product, informative advertising attempts to influence a user's choice. The ability for users to verify any statement is crucial. Consumers are therefore more likely to trust a company when they are informed, and this increases the authority of brands.

Learn more about informative advertisement

brainly.com/question/15734780

#SPJ4

4 0
1 year ago
Other questions:
  • Select all that apply.
    9·1 answer
  • In fiberoptic cable, the signal source is__________waves
    7·1 answer
  • EDVAC stands for? on which theory it is made on?
    15·1 answer
  • IT professionals recognize that successful systems must be user-oriented, and users need to be involved, formally or informally,
    6·1 answer
  • Consider a pipelined than can issue up to one instruction per cycle, but fewer may be issued because of pipeline hazards, multi-
    14·1 answer
  • Free up disk space by doing____?​
    6·2 answers
  • Create a for-loop which simplifies the following code segment: penUp(); moveTo(100,120); turnTo(180); penDown(); moveForward(25)
    5·1 answer
  • A merchant bank and a merchant have been involved in a Web-based electronic transaction. Which of the following mediates between
    9·1 answer
  • Why are computer simulations useful in studying phenomena in the universe?
    15·1 answer
  • Write the function definition for a function called list_total that accepts a list of integers
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!