user7329039
user7329039

Reputation: 77

How do I make my program count the number of sentences starting with a capital letter in a string?

My program currently only counts the number of capital letters in the whole string, not the ones after a period mark.

Desired output:

Enter essay:

I like Cats. Hey.

Sentences starting with capital letter: 2

Current output:


Enter essay:

I like Cats. Hey.

Sentences starting with capital letter: 3


Here's my code so far:

 static int sentencesChecker(String shortEssay) {



    int count = 0;

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

        if (isUpperCase(shortEssay.charAt(i))       ) {

           count++;

        }


    } System.out.println("Sentences starting with capital letter: " + count);

    return count;

}


public static void main(String[] args) {
    Scanner input = new Scanner (System.in);
    System.out.println("Enter essay: ");
    String essay = input.nextLine();

    sentencesChecker(essay);
}

Upvotes: 0

Views: 372

Answers (3)

Kiran Kumar
Kiran Kumar

Reputation: 1051

Here is the code.

  String str ="I like Cats. Hey.";
  //Scanner s = new Scanner(System.in);
  //System.out.println("Would you like to play again?");
  String[] strs = str.split("[.][ ]");
  int count =0;
  for(String string : strs){
      if(Character.isUpperCase( str.charAt(0) )){
          count++;
      }
  }
  System.out.println("the count :"+count);

Upvotes: 0

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

You can use a regular expression:

A regular expression, regex or regexp (sometimes called a rational expression) is, in theoretical computer science and formal language theory, a sequence of characters that define a search pattern. Usually this pattern is then used by string searching algorithms for "find" or "find and replace" operations on strings.

The regular expression to use in this context is:

^[A-Z]|\. *[A-Z]

That regular expression means (underlined the portion described in the right):

^[A-Z]|\. *[A-Z] Any uppercase letter from A to Z at the starting of line
------

^[A-Z]|\. *[A-Z]   or
      -

^[A-Z]|\. *[A-Z]   The character . followed by any number of 
       ---------   spaces, followed by an uppercase letter in the range A to Z

This can be used as follow in java

public int countSentencesStartingWithUppercase(String line) {
    String regexp = "^[A-Z]|\\. *[A-Z]"; // Note the additional \ this is 
                                         // done because \ is a special 
                                         // character in strings

    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(line);
    int count = 0;
    while (matcher.find()) {
        count++;
    }

    return count;
 }

Here a link to the tutorial on regular expressions in java.

Upvotes: 2

SomeJavaGuy
SomeJavaGuy

Reputation: 7357

Some more easy way than counting over the char array of the String would probably be the usage of String#split:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter essay: ");
    String essay = input.nextLine();
    String[] Uppcasesentences = essay.split("\\.\\s*[A-Z]");
    if(Uppcasesentences[0].matches("^\\s*[A-Z].*")) {
        System.out.println("You have got " + essay.split("\\.\\s*[A-Z]").length + " sentences starting uppercase");
    }
    else {
        System.out.println("You have got " + (essay.split("\\.\\s*[A-Z]").length-1) + " sentences starting uppercase");
    }
}

O/P

Enter essay: 
Sentence 1. sentence 2. Sentence 3. Sentence 4. sentence 5. Sentence 6
You have got 4 sentences starting uppercase

What is happening here is, the regex splits the String on each occasion of a dot followed by 0-n whitespaces followed by an uppercase letter. The length of the array you did just created should equal the amount of sentences starting uppercase now.

Edit: the split did ignore the first sentence, and would produce 2 for the input sentence 2. Sentence 2. Checking if the first array element starts with uppercase now. If not subtract 1.

Upvotes: 3

Related Questions