CS51 - Fall 2009 - Lecture 31

  • http://www.isi.edu/natural-language/people/knight3.html

  • counting occurrences of a word in a string
       - public int wordCount(String text, String word){
          int count = 0;
          int index = text.indexOf(word, 0);

          while ( index >= 0 ){
             index = text.indexOf(word, index + 1);
             count++;
          }
        return count;
       }
       
       - what if we wanted non-overlapping occurrences?
          - index = text.indexOf(word, index + word.length());

  • characters
       - char: another built-in type (like int, double, ...)
          - char c = 'a';
          - char c = "this is a string".getChar(7);
          - char[] charArray = "this is a string".toCharArray();
       - characters have integer values
          - what can we do with Integer?
             - >, <, >=, <=, ...
             characters have a defined sequence a...z, A...Z, 0...9
       - Character class has some useful static methods (like Integer or Double)
          - http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Character.html
          - Character.isUpperCase(char c)
          - Character.isWhiteSpace(char c);
          - Character.isLetter(char c);
          - Character.isLetterOrDigit(char c);
          - Character.isDigit(char c);

  • counting the number of sentences
       - For our purposes a sentence is:
          - a string of characters ending in a period followed by a space (or the end of the string)
          - the last word in the sentence should not be capitalized

       - public int sentenceCount(String text){
          String[] words = text.split(" ");
          int count = 0;

          for( String word: words ){
             if( word.endsWith(".") ){
                char first = word.getChar(0);

                if( !(first >= 'A' && first <= 'Z' ){
                // if( Character.isUpperCase(first) )
                   count++;
                }
             }
          }
       }


  • TP2: Pacman
       - read the whole description!
       - extensions
       - design
          - due on 11/25 at 10am
          - won't grade until we grade your final submission