
Untitled
By: a guest on
May 2nd, 2012 | syntax:
None | size: 1.89 KB | hits: 18 | expires: Never
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner kb = new Scanner(System.in);
int count = 0;
boolean good = false;
String phrase;
String text;
System.out.println("Enter some text to search in. Enter a blank line to stop.");
text = kb.nextLine();
while (good == false)
{
String word = kb.nextLine();
if (word.length() == 0)
{
good = true;
}
text += word;
}
System.out.print("Enter a phrase to search the above text for: ");
phrase = kb.nextLine();
while (phrase.length() < 1)
{
System.out.print("Please enter a non-blank phrase: ");
phrase = kb.nextLine();
}
String phrase1 = phrase.toLowerCase();
String text1 = text.toLowerCase();
for (int cntr1 = 0; cntr1 < text.length(); cntr1++)
{
if (text1.charAt(cntr1) == phrase1.charAt(0))
{
for (int cntr2 = 0; cntr2 < phrase.length(); cntr2++)
{
if (text1.charAt(cntr1 + cntr2) != phrase1.charAt(cntr2))
{
break;
}
else if (cntr2 == phrase1.length() - 1)
{
count++;
}
}
}
}
if (count == 1)
{
System.out.println("The phrase \"" + phrase + "\" was found " + count + " time.");
}
else
{
System.out.println("The phrase \"" + phrase + "\" was found " + count + " times.");
}
}
}