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
A bot can use a _______to capture keystrokes on the infected machine to retrieve sensitive information.
fgiga [73]

Answer:

keylogger.

Explanation:

A keylogger can be defined as a software program or hardware device designed for monitoring and recording the keystrokes entered by an end user through the keyboard of a computer system or mobile device.

Basically, a keylogger can be installed as a software application or plugged into the USB port of a computer system, so as to illegally keep a record of all the inputs or keys pressed on a keyboard by an end user.

In order to prevent cyber hackers from gaining access to your keystrokes, you should install an anti-malware software application such as Avira, McAfee, Avast, Bitdefender, Kaspersky, etc.

An antivirus utility is a software application or program that's used to scan, detect, remove and prevent computer viruses such as Malware, Trojan horses, keyloggers, botnets, adwares etc.

In conclusion, a bot can use a keylogger to capture keystrokes on the infected machine to retrieve sensitive information.

3 0
3 years ago
Hardware is defined as the physical parts of a computer
ddd [48]
Indeed, this is to be true as software is to programs and applications(apps) hardware is to physical computers such as hard drive, SSD, motherboard, ram, graphics card, ect...
8 0
2 years ago
Imagine that a you and a friend are exercising together at a gym. Your friend suddenly trips and falls, and it is clear that he
ladessa [460]

Answer:

The sprain happened when the friend fell and the ligaments (in the ankle)  stretched, twisted or possibly tore. Sprain is manifested by pain, swelling, bruising and inability to move.

Explanation:

Here the appropriate steps to stabilize the injury:

1.       Call for help.

2.       Rest the injured area to avoid further damage.

3.       Put ice ( for 15 to 20 minutes) to help limit the swelling.

4.       Apply compression bandage to prevent more swelling.

5.       Elevate the injured ankle above the heart to limit swelling.

Hope this helps UvU

4 0
2 years ago
When purchasing a security program, you must make sure that _____. Select 2 options.
Oliga [24]

Answer:

1 ans 3

Explanation:

3 0
2 years ago
For a biology presentation, Sofia wants to summarize the various body systems and their key functions. Which element can she use
harkovskaia [24]
I still say its table and bullet lst

7 0
3 years ago
Read 2 more answers
Other questions:
  • Mail merge requires an MS Word document in order to work.<br><br> True<br> False
    13·2 answers
  • You use a ____ following the closing brace of an array initialization list.
    12·2 answers
  • If you were going to construct a table that only included shape names with number prefixes, which shape would you include
    11·1 answer
  • Speakers wanting to control the movement of one slide to the next choose this option.
    9·1 answer
  • you'll be organizing the various databases in your organization, and have recommended hiring a/an?A. internet specialist B. comp
    6·2 answers
  • A(n) _________ is a computer system which is part of a larger system which performs a dedicated function.
    8·1 answer
  • Which button should be utilized if a user is unsure whether or not they are the right person to reply to an email?
    6·2 answers
  • Write method reverseString, which takes a string str and returns a new string with the characters in str in reverse order. For e
    8·1 answer
  • Given positive integer n, write a for loop that outputs the even numbers from n down to 0. If n is odd, start with the next lowe
    8·1 answer
  • What Security Issu E Commerce ?​
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!