TopcoderArchiveVisit Topcoder
SRM10 Apr 2003
SRM 141

Statements

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given a file which contains source code from a very simple programming language. Your task is to count the statements that are in the file. A statement is defined as 0 or more non-semicolon characters followed by a semicolon. For example, "hi;", "print one two three;", and ";" are all valid statements.

You will be given a String[] input, which represents the file that contains the code. Each String represents a line of text in the code. Note that a statement is not necessarily mapped to each line of text. A statement may span multiple lines of text, or a line of text may have multiple statements or parts of multiple statements. To ensure that all the text is contained in a valid statement, the last String will end with a semicolon.

You should return an int, which is the number of statements in the text.

Definition

Class:
Statements
Method:
counter
Parameters:
String[]
Returns:
int
Method signature:
int counter(String[] input)
(be sure your method is public)

Notes

  • The text in the code does not need to be syntactically correct or compilable code.

Constraints

  • input will contain between 1 and 50 elements, inclusive.
  • Each element of input will contain between 1 and 50 characters, inclusive.
  • Each element of input will only contain alphanumeric characters ('A'-'Z', 'a'-'z', '0'-'9'), spaces, or semicolons ';'.
  • The last element of input will end with a semicolon.

Examples

  1. {
     "set i 0;",
     "set array j",
     "  0 1 2 3 4 5",
     "  6 7 8 9 10;",
     "set k 1; set m 2;"
    }
    Returns: 4
    The first statement is on the first line of text. The second statement is on the second, third, and fourth lines of text. The last two statements are on the last line of text.
  2. {
     "print hello;; ;"
    }
    Returns: 3
    Although some of the statements in this example don't do anything, they are still valid statements.
  3. {
     "int 5;"
    }
    Returns: 1
    This wouldn't compile in any programming language, but it should still be a statement according to your program.
  4. {
     "aaaaddadgniv;aieaser;liusnetlseiru;slseriul",
     "asf;lnaselsfns;leiranser;laisenranserli;",
     "sflieurseilr;nasvlihase;rliuasern;"
    }
    Returns: 10
Back to all problems