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]
2 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]2 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
HELP PLS TIME LIMIT HERE
Novosadov [1.4K]
I do it immediately, so it don’t effect my assignments or grades.
7 0
3 years ago
como la ciencia y tecnologia ayudan a reducir la contaminacion del aire que respiramos? Muestra algunos ejemplos
Llana [10]
  • Mediante la tecnología se han implementado el uso de medios de transporte como los "coches verdes" o eléctricos que contribuyen a disminuir la producción de  gases contaminantes atmosféricos.

A través de la ciencia y la biotecnología se ha avanzado en el campo de la purificación del aire algunas de las herramientas que actualmente se están utilizando son:

  • La biofiltraccion del aire que se realiza a través de macroporos de material filtrante por donde se filtran los compuestos orgánicos e inorgánicos atmosféricos. También es posible realizar la biodepuracion del aire con plantas purificantes y ornamentales.

  • El uso de las paredes arbóreas cubiertas de musgo ha permitido que en algunas ciudades como París y Berlín mejore la calidad del aire,ya que el musgo se caracteriza  por su gran capacidad para  filtrar contaminantes del aire como las partículas y los óxidos de nitrógeno ya que  los convierte en purificadores naturales ideales para el medio ambiente.

Lo anterior son algunos de los grandes avances que ha tenido la ciencia y la tecnología para lograr mejorar la calidad del aire y por consiguiente la calidad de vida de las personas.

Puedes encontrar mas información sobre este tema en el siguiente enlace:

brainly.com/question/1400149

4 0
2 years ago
Select the function of keypunches that were used as one of the earliest input devices. A. It was used to control the cursor on t
natali 33 [55]

Answer:

Select the function of keypunches that were used as one of the earliest input devices.(1 point) -It was used for punching holes in the paper at relevant ...

Explanation:

3 0
1 year ago
Who is the father of modern computer?​
nydimaria [60]

Answer:

I would say Alan Turing is the father of the modern computer

7 0
3 years ago
Read 2 more answers
What are 3 examples of Chassis Wiring?
Roman55 [17]

Answer:

.

Explanation:

5 0
3 years ago
Other questions:
  • Which step of creating a financial budget involves listing the payroll, rental, and utility costs?
    10·2 answers
  • Web pages with personal or biograpic information are called ​
    10·1 answer
  • When choosing a new computer to buy, you need to be aware of what operating it uses.
    12·1 answer
  • Does any one play sniper clash 3d ??? ​
    9·2 answers
  • In UNIX, how do I set the permissions on MyProgram.py to: rwx---r-x?
    7·1 answer
  • If you were to create a new app for a smartphone or tablet that does not already exist, what would you create?
    10·1 answer
  • Examine the following algorithm.
    9·1 answer
  • What percentage of business are using social media today
    14·1 answer
  • Assume that a 5 element array of type string named boroughs has been declared and initialized. Write the code necessary to switc
    5·1 answer
  • True or False? Popular sites are always mean accurate.
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!