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
10.13 lab 10c: output range with increment of 5 write a program whose input is two integers. output the first integer and subseq
Lynna [10]

he program gives an output in increment of 5, if the first inputted integer is less than the second. The program written in python 3 goes thus

3 0
2 years ago
I WILLLLL CHOOOOSEEEE YOU ASSS BRAINLESTTT!!! , HELLPPPPP MEEEE DUEEEE TODAYYYYYY!!!!
Ivenika [448]

Answer:

rohot will not start well when we try to start .....they will make a unique sound or a noise sound like something wrong inside ...reduce their body flexibility. they will not able to move or do works well..loss of jobs ..and opportunities. .

Explanation:

I hope it will help u ....

6 0
3 years ago
Read 2 more answers
if a user watches a video with event tracking three times in a single session, analytics will count how many unique events?
Lyrx [107]

Answer:

1

Explanation:

Analytics will count it a One (‘1’) Unique Event, even if a user watches a video with event tracking three times but in a single session.

8 0
2 years ago
What is a registered degree
dezoksy [38]

Answer:

I not sure

Explanation:

I think it's when a college student gets a degree for a job or something??? I dont know. I to young to know ;-;

lol

5 0
3 years ago
The relational algebra expressions can be rearranged and achieve the same result but with a better processing time. A selection
BigorU [14]

Answer:

c. This would reduce the number of tuples to involve in the join resulting in a more efficient query.

Explanation:

SQL or structured query language is a database querying language used to communicate or interact with databases. A database query could be as simple as returning all the columns from a database table to complex like joining several query results which in turn forms an algebra expression.

Every query statement or expression is executed with time, execution time needs to be minimized for efficiency, so, a well-arranged and reduced joining in the query improves the efficiency of the query.

8 0
3 years ago
Other questions:
  • ................njkkjjiooiuuuu
    6·1 answer
  • Which class of fire extinguisher is appropriate for a fire involving electrical/energized electrical equipment?
    13·2 answers
  • _________ cards contain a chip that can store a large amount of information as well as on a magnetic stripe for backward compati
    12·1 answer
  • 6. What are the two keyboard keys you can press at the same time to use the Copy command? (1.0 points)
    12·1 answer
  • Convert 1001101012 to base 10
    5·1 answer
  • A group of cells is called a<br><br> Excel
    5·1 answer
  • Henry is planning to visit Spain for a vacation. He wants to learn Spanish, but he prefers to do it in a fun way. He searches on
    8·2 answers
  • An enhancement to a computer accelerates some mode of execution by a factor of 10. The enhanced mode is used 50% of the time, me
    11·1 answer
  • It is a read mostly memory that can be electronically written into any time without arising prior contact
    12·1 answer
  • Power Supply<br><br> Description :
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!