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]
2 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]2 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
Computer network reduces the cost. explain the statement with example.​
Crank

Answer:

Justify this statement with an example. Ans: Computer Network reduces expenses of an office because computer on a network share different hardware devices like printers, scanners, hard disk etc. And sharing those hardware devicAes will reduces the expense of an office.

Explanation:

Justify this statement with an example. Ans: Computer Network reduces expenses of an office because computer on a network share different hardware devices like printers, scanners, hard disk etc. And sharing those hardware devicAes will reduces the expense of an office.

3 0
3 years ago
In cell G6, use the appropriate lookup and reference function to retrieve the rental rate from the named range RentalRates. The
lora16 [44]

=VLOOKUP($A6,RentalRates,2,0) is the lookup for this.

<u>Explanation:</u>

Utilize the LOOKUP capacity to look into an incentive in a one-section or one-push go, and recover an incentive from a similar situation in another segment or one-push go. The query work has two structures, vector and cluster. This article depicts the vector structure.

In computer science, a lookup table is a cluster that replaces run time calculation with a more straightforward exhibit ordering activity. The investment funds as far as handling time can be critical, since recovering an incentive from memory is regularly quicker than experiencing a "costly" calculation or information/yield activity.

5 0
2 years ago
50 POINTS How do you express yourself and your creativity through computer science?
Igoryamba

Answer:

1. I express myself through the designing, development and analysis of software and hardware used to solve problems in a variety of business, scientific and social contexts.

2. support the development of general reasoning, problem-solving and communication skills.

3. Through avoiding in repetious tasks and simplifying such works.

3 0
2 years ago
Please help with attached file
Paul [167]
Where is the attached file?
8 0
3 years ago
Read 2 more answers
Which of the following is true of two-factor authentication?
dybincka [34]

Answer: D)It relies on two independent proofs of identity.

Explanation: Two factor authentication is technique which uses two steps/stage for the verification or authentication.It is done for the extra security maintenance.IT is also known as 2FA. It consist of two different authentication component so that it penetrates through double protection layer .

Other options are incorrect because It RSA public key cannot be used for the authentication of private content. It does not use both hand geometry at same time for both the levels and It does not utilize only single sign-on technology. Thus, the correct option is option(D).

7 0
3 years ago
Other questions:
  • Food is shipped thousands of miles throughout our country using various types of transportation such as trucks, planes, and boat
    10·1 answer
  • A typical serial cable has
    13·1 answer
  • Can you know what time someone retweeted
    5·1 answer
  • In Alphatech Systems, email software screens incoming messages by organizing them into mailboxes and identifying junk mail. As a
    13·1 answer
  • What options of the hierarchical star topology within the ANSI/TIA-568-C allow the ability to move the workgroup switches closer
    7·1 answer
  • How does emotional awareness help you with non-verbal communication?
    13·2 answers
  • What is the TAG to begin a Web page, as recommended by the W3C?
    13·1 answer
  • A browser is an example of a. :
    7·1 answer
  • Examples of email use that could be considered unethical include _____.
    14·2 answers
  • Write a program that will remove "May" from the list using .Pop and .Index methods.(5pts) GIVEN: lst=["January", "February", "Ma
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!