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
Given a integer, convert to String, using String Builder class. No error checking needed on input integer, However do read in th
Gennadij [26K]

Answer:

The program in Java will be:

// Java program to demonstrate working parseInt()  

public class GFG  

{  

   public static void main(String args[])  

   {  

       int decimalExample = Integer.parseInt("20");  

       int signedPositiveExample = Integer.parseInt("+20");  

       int signedNegativeExample = Integer.parseInt("-20");  

       int radixExample = Integer.parseInt("20",16);  

       int stringExample = Integer.parseInt("geeks",29);  

 

       // Uncomment the following code to check  

       // NumberFormatException  

 

       //   String invalidArguments = "";  

       //   int emptyString = Integer.parseInt(invalidArguments);  

       //   int outOfRangeOfInteger = Integer.parseInt("geeksforgeeks",29);  

       //   int domainOfNumberSystem = Integer.parseInt("geeks",28);  

 

       System.out.println(decimalExample);  

       System.out.println(signedPositiveExample);  

       System.out.println(signedNegativeExample);  

       System.out.println(radixExample);  

       System.out.println(stringExample);  

   }  

}

7 0
3 years ago
Please
levacccp [35]

Answer:

inspect

Explanation:

7 0
2 years ago
how many usable host addresses are available for each subnet when 4 bits are borrowed from a class C IP address
jonny [76]

Answer:

The answer is "14".

Explanation:

Let the IP address = 196.45.204.0

When it borrowed 4 bits

\therefore\\\\ subnet = 28

IP=  \frac{196.45.204.0}{28}\\\\ 28 \to 11111111.11111111.11111111.11110000

If the borrowed bits are left out then:

The Number of useable host addresses:

= {(2^4) - 2} \\\\ = 16-2\\\\ =14

3 0
2 years ago
Debate whether social networking is harmful or helpful to society.
Dmitriy789 [7]

Answer:

I believe that Social Networking <em>IS </em>helpful to society, but, people use it for wrong stuff.

5 0
1 year ago
I think i have a virus on my computer what am i supposed to do
patriot [66]

Answer:

call like a phone or computer company and ask wjat thry can do

4 0
3 years ago
Other questions:
  • Which of the following is a narrative essay most like
    8·2 answers
  • When uninstalling software, it is best to delete the folder containing the software?
    11·1 answer
  • Pointsyour company environment includes windows server versions 2003, 2008, and 2012. desktops range from windows xp and vista.
    13·1 answer
  • Sam has installed a new CPU in a client’s computer, but nothing happens when he pushes the power button on the case. The LED on
    14·1 answer
  • Which two technologies support the building of single-page applications? and are two technologies helpful in building single pag
    12·1 answer
  • You just bought a hard drive, you plan on using this a secondary storage, once installed, what needs to be done to the drive and
    6·1 answer
  • Describe at least two other companies that are direct or indirect competitors to your company. Explain how you will differentiat
    15·1 answer
  • I have been charged for brainly plus yet I still have to watch ads why? What do I do?
    12·2 answers
  • When backing up a database, what is added to the file name?<br> On g metrix
    9·1 answer
  • Create a method called letterGrade that will pass the student's average grade as a parameter and return a letter grade (char). O
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!