0% found this document useful (0 votes)
6 views1 page

WordCounter Java

script of word counter

Uploaded by

Diptanshu Rajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

WordCounter Java

script of word counter

Uploaded by

Diptanshu Rajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

package col106.assignment4.

HashMap;

public class WordCounter {

public WordCounter(){
// write your code here
}

public int count(String str, String word){


int answer=0;

int str_length=str.length();
int word_length=word.length();

int size = str_length-word_length+1;

HashMap hmap_wc = new HashMap(size);

String temp=null;

for(int i=0; i+word_length<str_length+1;i++){

temp = str.substring(i,i+word_length);

if(hmap_wc.contains(temp)){

hmap_wc.put(temp,(int)hmap_wc.get(temp)+1);
}

else{
hmap_wc.put(temp,1);
}
}

if(!hmap_wc.contains(word)) answer=0;
else{
answer = (int)hmap_wc.get(word);
}

return answer;
}
}

This study source was downloaded by 100000805284328 from CourseHero.com on 10-30-2024 12:54:18 GMT -05:00

https://www.coursehero.com/file/66282062/WordCounterjava/
Powered by TCPDF (www.tcpdf.org)

You might also like