answer.
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]
1 year 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]1 year 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
Choose the response that best completes the following statement.
lisabon 2012 [21]
I am guessing. My guess is code.
7 0
1 year ago
Read 2 more answers
When Liam went to print his presentation, the boot process established the connection to the printer, sent the presentation to t
Mars2501 [29]

Answer:

b. False

Explanation:

When Liam went to print his presentation, the boot process established the connection to the printer, sent the presentation to the printer, and let other software know the printer was busy. It is a false statement.

6 0
2 years ago
When the computer is started, a bootstrap or IPL (Initial Program Load) begins testing the system. Where is this bootstrap progr
aniked [119]

Answer:

The correct answer to the following question will be Option B (ROM).

Explanation:

Bootstrap is the programming which initializes the OS during initialization. The bootstrap loader creates small operator programs that connect and monitor the various computer hardware components.

ROM is Read-only memory, which will be used to hold a computer's start-up commands, also recognized as firmware.

No other method was used to help save system startup. ROM, then, is the right answer.

7 0
1 year ago
When a product owner adds a new feature/idea in the backlog and brings it up for discussion during refinement session, how shoul
Drupady [299]

Answer:

2 is the correct answer.

5 0
2 years ago
Read 2 more answers
Recall that a Set is an abstract data type somewhat similar to a Bag, they can store a finite collection of objects without any
lina2011 [118]

Answer:

Here is the addLikeASet() method:

public boolean addLikeASet(T anEntry){  // method takes the T parameter anEntry

       boolean res = true;  //sets the result to true

       if(Arrays.asList(bag).contains(anEntry);  {  //checks if the bag already contains anEntry

           res=false;         }  //returns false

       else         {   if the bag does not already contain anEntry

           bag[numberOfEntries]=anEntry;  //enters the anEntry in bag array

           numberOfEntries++;         }  //increments the  numberOfEntries by 1

       return res;     } //returns true

Explanation:

You can also implement this method like this:

public boolean isFilled(){ // the function to check if the bag is full

       return numberOfEntries == bag.length;  } // number of entries equals length of the bag which shows that the bag is full

public boolean addLikeASet(T anEntry){

       boolean res=true;

       if (!isFilled() || (Arrays.asList(bag).contains(anEntry));  //checks if the bag is full or contains anEntry already

           res=false;         //returns false

       else         { if bag does not already contain anEntry

           bag[numberOfEntries]=anEntry; //add anEntry to the bag

           numberOfEntries++;         } add 1 to the count of numberOfEntries

       return res;     } //returns true

You can also use a loop to check if anEntry is already present in bag:

for(int i=0; i< bag.length; i++)  {

    if(anEntry.equals(bag[i])) {

      return false;      }    }  

  return true;

3 0
2 years ago
Other questions:
  • Using information from the lesson, explain how new technologies change your experience as a consumer.
    5·2 answers
  • How frequently should computers containing ehr information be backed up?
    14·1 answer
  • Which type of utp cable is used to connect a pc to a switch port?
    10·1 answer
  • Who are the founders of video-sharing site Dailymotion?
    7·1 answer
  • To gain experience of using and combing different sorting algorithms: election sort, insertion sort, merge sort, and quick sort.
    14·1 answer
  • If you are viewing a webpage with customized or regenerated content, such as updated stock quotes, what type of webpage are you
    5·1 answer
  • In this exercise, you will create a couple of helper methods for ArrayLists in a class called ArrayListMethods.
    10·1 answer
  • A school principal trying to find out if parents will help buy new playground equipment shows digital leadership by.
    8·2 answers
  • A bicycle sharing company is developing a multi-tier architecture to track the location of its bicycles during peak operating ho
    11·1 answer
  • A data scientist is writing a Machine Learning (ML) algorithm using a large data set.
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!