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
Finding your style in photography is about ___
marusya05 [52]
Only B and C would be the best choice
7 0
3 years ago
Read 2 more answers
What are the chief contributions of philosophy to artificial intelligence?
olga nikolaevna [1]

Answer

Hi,

The chief contribution of philosophy to artificial intelligence is the knowledge of the connections between the two and the understanding of the shared concept.

Explanation

Philosophy and Artificial intelligence has a close scientific connection because they both share concepts such as action, consciousness, epistemology and free will. From the artificial intelligence perspective, theories in philosophy are important in AI as long as they provide the basis of the designs, reasons and plan. The concepts shared in AI con tribute to the realization of the philosophy of artificial intelligence.

Hope this Helps!

5 0
3 years ago
I am having horrible trouble with deciding if I should get Audacity or Adobe Spark for recording, if someone could help me choos
lbvjy [14]

I haven't really used either, but people I know would prefer using Audacity.

5 0
3 years ago
Read 2 more answers
list out the application of the computer and explain it?(and as the answer for this question as image) ​
amid [387]

Answer:

There are several applications of the computer; some of these applications are:

  • Word processors
  • Graphics applications
  • Web browsers
  • Gaming
  • Media players
  • Alarms and tasks schedulers
  • Etc...

Explanation:

The computer system can be applied in various fields (such as medicine, gaming, governments, education, etc.) for various purposes.

Each of these fields require computer software products and/or applications to carry out their activities. I've listed some applications, but there are several other applications.

<em>My explanation could not be submitted; so, I added it as an attachment.</em>

Download txt
3 0
3 years ago
How do you code to find the surface area 6 s2, volume s3 in python
Nana76 [90]

Answer: you code to find the surface area 6 s2, volume s3 in python

Explanation:if you dont code the surface area 6 s2, volume s3 in python then you  cant code

4 0
3 years ago
Other questions:
  • Write a java program called allDigitsOdd that returns whether every digit of a positive integer is odd. Return true if the numbe
    6·1 answer
  • The LTE (cellular telephone) standard supports only packet switching"". What cellular services are morst affected by this change
    15·1 answer
  • The malicious use of computer code to modify the normal operations of a computer or network is called a ___ .
    13·1 answer
  • A formula =A1+B2 is in cell D8. If you copy that formula to cell D9, what is the new formula in cell D9? A. '=A1+B2 B. '=A2+B3 C
    8·2 answers
  • (Java)
    12·1 answer
  • What is transmission control protocol?
    9·2 answers
  • 1. Consider a direct-mapped cache that can accommodate 8Mbytes from a main memory, that uses a 32-bit address and 32-byte blocks
    5·1 answer
  • Write a loop that finds the sum of the numbers between 7 and 34
    11·1 answer
  • Your iphone is not configured to allow calls using this mac
    10·1 answer
  • Execute and explain this program using // in Layman's language. Thank you!
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!