/* related posts with thumb nails */

A program to read a text and count all the occurrences of a given word. Also, display their positions.:

import java.io.*;

class TextSearch

{

public static void main(String as[]) throws Exception

{

InputStreamReader isr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(isr);

int i=0,count=0;

String text="",s="";

System.out.println("Enter Text:(press ENTER twice to stop)\n");

s=br.readLine();

while(s.length()!=0)

{

text+=s;

s=br.readLine();

}

System.out.println("Enter search word:");

s=br.readLine();

while(true)

{

i=text.indexOf(s,i);

if(i==-1) break;

System.out.println("Word found at position:"+i);

count++;

i+=s.length();

}

System.out.println("Number of occurrences of given word:"+count);

}

}

/* Output: */

Enter Text:(press ENTER twice to stop)

Hello hi , hi again

hi nice to see you

Enter search word:hi

Word found at position:6

Word found at position:11

Word found at position:19

Number of occurrences of given word:3

Related Topics:

1 comments:

Theodor said...

Great service is just around the corner with helpful mortgage information and valuable resources to make buying your property quick and easy. canada mortgage calculator If we paid that $1,000 with the end with the fifth year, we save $ 3449 in interest. canada mortgage calculator

Post a Comment