TopcoderArchiveVisit Topcoder
SRM28 Jun 2002
SRM 102

Phrase

Problem statement, definition, constraints, and public examples.

Problem Statement

PROBLEM STATEMENT

A local high school English teacher has the unique inability to count words in
papers that his students turn in. Thus, he has asked you to devise an algorithm
to count words. Although the input size is quite limited, this teacher has
remarkable patience and is willing to input everything into your algorithm a
short phrase at a time. A phrase is constructed of words intermixed with
punctuation and spaces. A word is a contiguous series of letters and/or
apostrophes. 

Create a class Phrase which contains a method numwords which takes a phrase in
the form of a String as a parameter and determines how many words are contained
in the phrase. Of the punctuation allowed, only the apostrophe (''') does not
delimit words. Thus, the phrase "Don't you,think so?" contains 4 words:
"Don't", "you", "think", and "so".

DEFINITION

Class name: Phrase
Method name: numwords
Parameters: String
Returns: int

The method signature is (make sure it is declared public): int numwords(String
input);

TopCoder will ensure the validity of the inputs.  Inputs are valid if all of
the following criteria are met:
* input will be between 1 and 50 characters in length, inclusive.
* input will contain only letters (A-Z, a-z), spaces, and the following
punctuation: period '.', comma ',', exclamation point '!', colon ':', semicolon
';', question mark '?', and apostrophe '''.

EXAMPLES

1. "this is a test" contains 4 words, and thus returns 4.
2. "Hello world a to two four three eighth seventh" contains 9 words, and thus
returns 9.
3. "!" contains no full words, and returns 0.
4. "Another   test,  with  punctuation... " returns 4.
5. "Don't you think so?" returns 4.
6. "'" returns 1.

Definition

Class:
Phrase
Method:
numwords
Parameters:
String
Returns:
int
Method signature:
int numwords(String param0)
(be sure your method is public)

Constraints

    Examples

      No public sample cases were explicitly marked for this problem in the original data.

      Back to all problems