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
What is mean by computer ?where it is used?​
dem82 [27]

Answer:

computer is a set consisting of hardware and software that perform specific set of instructions

Explanation:

thy are used in hospitals, institute,hotels

4 0
3 years ago
You will need to complete the functions these functions: a. get_total This function takes in pennies, nickels, dimes, and quarte
tankabanditka [31]

Answer:

penny = int(input("Enter number of pennies: "))

nickel = int(input("Enter number of nickel: "))

dime = int(input("Enter number of dime: "))

quarter = int(input("Enter number of quarter: "))

def get_total(penny, nickel, dime, quarter):

   total = ((1 * penny) + (5 * nickel) + (10 * dime) + (25 * quarter))/ 100

   print (f"The total amount is {total}")

   

def get_dollars(penny, nickel, dime, quarter):

   dollar = ((1 * penny) + (5 * nickel) + (10 * dime) + (25 * quarter))// 100

   print (f"The dollar part is ${dollar}")

   

def get_left_over_cent(penny, nickel, dime, quarter):

   cent = ((1 * penny) + (5 * nickel) + (10 * dime) + (25 * quarter)) % 100

   print (f"The left over cent is {cent} cents")

 

   

get_total(penny, nickel, dime, quarter)

get_dollars(penny, nickel, dime, quarter)

get_left_over_cent(penny, nickel, dime, quarter)

Explanation:

In the United States, these coins have following values:

Quarter= 25 cents

Dime= 10 cents

Nickel= 5 cents

Penny= 1 cent

The first line collect an input from the user, convert to integer and assign to penny.

The second line collect an input from the user, convert to integer and assign to nickel.

The third line collect an input from the user, convert to integer and assign to dime.

The fourth line collect an input from the user, convert to integer and assign to quarter.

Based on the above definition of penny, nickel, dime and quarter; we defined the various function.

First the get_total function was defined and the total amount was calculated and assigned to total, then the total was output to the user.

Then, the get_dollars function was defined and the dollar part was calculated through dividing the total by 100. (100 cents = $1). The dollar was also output to the user.

Then, the get_left_over_cents function was defined and the cent was calculated by finding the remainder when the total is divided by 100 (cent = total % 100). The symbol "%" represent modulo and is used to find remainder of a division operation.

6 0
3 years ago
Match the vocabulary to the appropriate definition.
Allisa [31]

Answer:

Pulling related code together in objects is Arrays

Helps programmers when they revisit code or are new to code-- Documentation

Explanation:

This is all I know

3 0
2 years ago
Kurt is editing a table in his powerpoint that displays accounting formulas and when to use them. he wants to remove the table s
Olenka [21]
The feature that Kurt should use is the split feature. 
7 0
3 years ago
Data refer to
Yanka [14]
D. Large sets of information that can be analyzed for patterns and trends
3 0
3 years ago
Other questions:
  • what is the opportunity cost of buying a $.75 soda everyday at school for 3 years instead of drinking water ? assume you are in
    10·2 answers
  • Can somebody please halp meeee?? I'm doing Web design.
    12·2 answers
  • What is not soured of income
    6·1 answer
  • Consider a Games Expo for Children. Six competitions are laid out for the expo. Tickets are sold according to the age and gender
    11·1 answer
  • ____________ provides the architect opportunity to electronically plan and place elements of a building.
    6·2 answers
  • What are the four elements of game design?<br><br> Help Me!!
    9·1 answer
  • Tim is creating a page to display his poker club’s set of rules. He has main categories and subcategories along with about a par
    12·1 answer
  • Evaluating algebraic expressions 8(x+2)when x =6
    7·1 answer
  • How does equal employment opportunity apply to nursing practice?​
    9·1 answer
  • Providing captions and transcripts for videos on your website is a way of ensuring what?.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!