answersLogoWhite

0


Best Answer

/*this block of code fineds any given string in tree different file

displays the string and prints the number of occurences in these file....thats all:)*/

package useToken;

import java.io.*;

class readFile extends StreamTokenizer {

readFile(InputStream in ){

super(in);

}

public static void main (String argv[]){

try {

int ret;

String string;

BufferedReader in =

new BufferedReader (newInputStreamReader(System.in));

System.out.print("Enter some text: ");

string = in.readLine();

String[] file = {"file1.txt","file2.txt","file3.txt"};

for(int i=0;i<3;i++){

FileInputStream fin = new FileInputStream(file[i]);

readFile rt = new readFile(fin);

int counter =0;

while((ret = rt.nextToken())!= 0 && rt.sval != null){

if(rt.sval.equals(string)){

System.out.println("Found Text :" + rt.sval );

counter++;

}

}

System.out.println("The String Found :" + counter + " " + "times");

System.out.println(file[i] + " " + "Complete");

}

} catch (Exception e ){

System.err.println("Exception :" + e);

}

}

}

Write a program to find the frequency of words in a sentence in java programing?

//program to find occurence of a word in a sentence

import java.io.*;

public class cnt2

{

public static void main(String args[])throws IOException

{

int times=0,count=0,x=0,no=0;

InputStreamReader ir=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(ir);

String s,w;

System.out.println("Enter the sentence:");

s=br.readLine();

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

w=br.readLine();

try{

for(int i=0;i

{

if(w.charAt(0)==s.charAt(i))

{

for(int j=0;j

{

if(s.charAt(i)==w.charAt(j))

{ count=count+1;}

if(count==w.length())

{no=no+1;count=0;};

}

}

}

catch(Exception e){}

if(no==0)

{

System.out.println("word is not present");

}

else

{

System.out.println("word is present "+no+" times");

}

}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

final File textFile = new File("File to Search.txt");

final BufferedReader in = new BufferedReader(new FileReader(textFile));

// Mapping of String->Integer (word -> frequency)

final TreeMap<String, Integer> frequencyMap = new TreeMap<String, Integer>();

// Iterate through each line of the file

String currentLine;

while ((currentLine = in.readLine()) != null) {

// Remove this line if you want words to be case sensitive

currentLine = currentLine.toLowerCase();

// Iterate through each word of the current line

// Delimit words based on whitespace, punctuation, and quotes

final StringTokenizer parser = new StringTokenizer(currentLine, " \t\n\r\f.,;:!?'"");

while (parser.hasMoreTokens()) {

final String currentWord = parser.nextToken();

Integer frequency = frequencyMap.get(currentWord);

// Add the word if it doesn't already exist, otherwise increment the

// frequency counter.

if (frequency == null) {

frequency = 0;

}

frequencyMap.put(currentWord, frequency + 1);

}

}

// Display our nice little Map

System.out.println(frequencyMap);

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include<stdio.h>

#include<conio.h>

int occurence(char *,int ,char *);

int main()

{

char str[30],str1[20];

int i,j=0,len,n=0;

printf("enter the sentence\n");

gets(str);

printf("enter the word\n");

gets(str1);

for(i=0;str1[i]!='\0';i++);

len=i--;

for(i=0;str[i]!='\0';i++)

{

if((str[i+1]==' ')(str[i+1]=='\0'))

{

n=n+occurence((str+j),len,str1);

j=i+2;

}

}

printf("occurence=%d",n);

getch();

}

int occurence(char *ptr,int m,char *ptr1)

{

int i,count=0;

for(i=0;i<=m;i++)

{

if(*(ptr+i)==*(ptr1+i))

{

count++;

}

}

if((count)==m)

{

return 1;

}

else

{

return 0;

}

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a Java program to find occurrences of given word in a text?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can we add website link when we write java program?

yes ,i can add the website link in java program when we write.


Write a program to change the case of the given string?

This functionality is already in Java. String.toLowerCase() and String.toUpperCase() will take care of it for you.


Write a java program to count the space from the given line?

.... String line = "This is example program with spaces"; String[] tokens = line.split(" "); System.out.println(tokens.length-1); .......


Write a c program Fibonacci series using for loop in java?

Exactly what do you mean by 'C program in Java'


Write a Java Program to find occurences of given word in a text?

Check the documentation of the String class, for a method that searches for a substring.


Write a java program to find sum of even and odd numbers in given array?

for(int i = 1; i &lt; 100; i+=2) { System.out.println(i); }


What is javadoc?

write a java program to display "Welcome Java" and list its execution steps.


How write new line program in java?

\n


How do you write a java program for finding multiligual languages?

You can use Java's built-in functions to write a code that will find multilingual languages.


How java is 100 percent Internet based?

It isn't. It can also be used to write desktop applications. Java does have many options to program for the Internet, but that is not the only possibility.It isn't. It can also be used to write desktop applications. Java does have many options to program for the Internet, but that is not the only possibility.It isn't. It can also be used to write desktop applications. Java does have many options to program for the Internet, but that is not the only possibility.It isn't. It can also be used to write desktop applications. Java does have many options to program for the Internet, but that is not the only possibility.


How do you write a java program to find the transpose of the matrix for the given elements?

You basically write a nested for loop (one for within another one), to copy the elements of the matrix to a new matrix.


Write a java code to draw a circle inside in an ellipse?

write a program draw circle and ellipse by using oval methods in java