How to count number of occurrences of pattern within a string?

Java Count 'Em Right?

  • Call your project CountEmRight and create just one class, Tester. In the main method, do the following: • Create a loop that asks for String input (a sentence). • Release from the loop if the input String is “EXIT”. • So as to ignore case, convert the input String into an uppercase version of itself. • Concatenate some “harmless” String to the end of the input String. By “harmless” it is meant that it should not contain any occurrences of the delimiter expression. This is the real secret to this project… to get any occurrence of the sought after expression off the end and into the “interior” of the String. • Use the split method to produce a String array (call it sp). Then use sp.length –1 to count the number of occurrences. A typical run will appear as follows: Type in a sentence and press ENTER. His initials are SA There are 2 occurrences. Type in a sentence and press ENTER. Sad but true, their teams are better. There are 2 occurrences. Type in a sentence and press ENTER. S a sa ssa s a There are 4 occurrences. Type in a sentence and press ENTER. exit

  • Answer:

    This is pretty simple. Do you have a specific question, or are you just expecting someone to do your homework for you? @M

McFate at Yahoo! Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.