I want to count the number of occurrences of particular word in a source string.
Let's say src="thisisamangoterrthisismangorightthis?"
word="this"
So what I am doing is, first search for index of word in src. It's at index 0. Now I am extracting the part from this index location to end of src.
i.e., now src="isamangoterrthisismangorightthis?" and search for word again.
But I am getting array out of bound exception.
public static int countOccur(String s1, String s2)
{
int ans=0;
int len1=s1.length();
int len2=s2.length();
System.out.println("Lengths:"+len1+" " +len2);
while(s1.contains(s2))
{
ans++;
int tmpInd=s1.indexOf(s2);
System.out.println("Now Index is:"+tmpInd);
if((tmpInd+len2)<len1){
s1=s1.substring(tmpInd+len2, len1);
System.out.println("Now s1 is:"+s1);
}
else
break;
}
return ans;
}
Try this to count the word in a string,
private static int countingWord(String value, String findWord)
{
int counter = 0;
while (value.contains(findWord))
{
int index = value.indexOf(findWord);
value = value.substring(index + findWord.length(), value.length());
counter++;
}
return counter;
}
When you use a method that throws ArrayIndexOutOfBoundsException, it's always a good idea to check the bounds. See String#substring:
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex
is larger than the length of this String object, or beginIndex is
larger than endIndex.
You should cover all cases:
if(tmpInd + len2 >= s1.length() || len1 >= s1.length() || ... ) {
//Not good
}
Or, better, you should consider your logic to avoid this situation in the first place.
Try and use indexOf(), it will take care of bounds etc for you:
public static int countOccurrences(final String haystack, final String needle)
{
int index = 0;
int ret = 0;
while (true) {
index = haystack.indexOf(needle, index);
if (index == -1)
return ret;
ret++;
}
// Not reached
throw new IllegalStateException("How on earth did I get there??");
}
Rather than doing a substring on your String use this method
public int indexOf(int ch, int fromIndex)
then just check if the result is -1
Your might use replace to solve the problem
String s = "thisisamangoterrthisismangorightthis?";
String newS = s.replaceAll("this","");
int count = (s.length() - newS.length()) / 4;
import java.io.*;
import java.util.*;
public class WordCount
{
public static class Word implements Comparable<Word>
{
String word;
int count;
#Override
public int hashCode()
{
return word.hashCode();
}
#Override
public boolean equals(Object obj)
{
return word.equals(((Word)obj).word);
}
#Override
public int compareTo(Word b)
{
return b.count - count;
}
}
public static void findWordcounts(File input)throws Exception
{
long time = System.currentTimeMillis();
Map<String, Word> countMap = new HashMap<String, Word>();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(input)));
String line;
while ((line = reader.readLine()) != null) {
String[] words = line.split("[^A-ZÅÄÖa-zåäö]+");
for (String word : words) {
if ("".equals(word)) {
continue;
}
Word wordObj = countMap.get(word);
if (wordObj == null) {
wordObj = new Word();
wordObj.word = word;
wordObj.count = 0;
countMap.put(word, wordObj);
}
wordObj.count++;
}
}
reader.close();
SortedSet<Word> sortedWords = new TreeSet<Word>(countMap.values());
int i = 0;
for (Word word : sortedWords) {
if (i > 10) {
break;
}
System.out.println("Word \t "+ word.word+"\t Count \t"+word.count);
i++;
}
time = System.currentTimeMillis() - time;
System.out.println("Completed in " + time + " ms");
}
public static void main(String[] args)throws Exception
{
findWordcounts(new File("./don.txt"));
}
}
Related
I want to search how many times a string appear in another string
It does not work correctly when i put an similar string at the end.
public class C3_Project3_WPr {
public static void main(String[] args) {
String strn1="AliAliAli";
String strn2="AliAliSinaAli";
String strn3="Ali";
int count1=StringCounter(strn1, strn3);
System.out.println(count1);
int count2=StringCounter(strn2, strn3);
System.out.println(count2);
}
//ُString in String Method
static int StringCounter(String str1, String str2){
int counter=0;
if (str1.isEmpty() || str2.isEmpty()) {
return 0;
}
for(int i= 0; i<str1.length(); i++){
if(str1.contains(str2)){
counter++;
str1= str1.substring(str2.length());
}
}
return counter;
}
}
Solution to your problem is here
public static void main(String[] args) {
String strn1 = "AliAliAliwewdwdweAli";
String strn2 = "AliAliSinaAliAlAli";
String strn3 = "Ali";
int count1 = StringCounter(strn1, strn3);
System.out.println(count1);
int count2 = StringCounter(strn2, strn3);
System.out.println(count2);
}
// ُString in String Method
static int StringCounter(String str1, String str2) {
int counter = 0;
if (str1.isEmpty() || str2.isEmpty()) {
return 0;
}
for (int i = str1.indexOf(str2); i >= 0; i = str1.indexOf(str2, i + str2.length())) {
counter++;
}
return counter;
}
}
When modifying str1 you only take in to account the length of the search string, but ignore the index in which it was found. Fixing this (e.g., by using indexOf), will fix your results too:
int index = str1.indexOf(str2);
while (index >= 0) {
counter++;
index = str1.indexOf(str2, index + str2.length());
}
Use recursive method: It's quick and easy way to solve your problem:
static int StringCounter(String str1, String str2){
return (str1.contains(str2)) ? 1 + StringCounter(str1.replaceFirst(str2, ""), str2) : 0;
}
public static int getNthOccurrence(int n, char find, String str)
{
int counter=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)==find)
{
counter++;
if(counter==n)
return i;
}
}
return -1;
}
I have already seen this thread Java: method to get position of a match in a String?
The code runs that:
int n=getNthOccurrence(3,'n',"you arent gonna find me");
output: 13
You can make a char into a String by simply doing this
String theString = yourChar + "";
Your code could look something like this !spoiler below!
public static int getNthOccurrence(int n, char find, String str)
{
String f = find + "";
int counter = 0;
for (String c : str.split("")){
if (c.equals(f)) n--;
if (n < 1 ) return counter;
counter++;
}
return -1;
}
}
Once you've changed the parameter type to String:
public static int getNthOccurrence(int n, String find, String str)
Use String.indexOf(String, int):
i = -find.length();
for (int count = 0; count < n; ++count) {
i = str.indexOf(find, i + find.length());
if (i < 0) break;
}
return (i >= 0) ? i : -1;
(Note that String.indexOf(char, int) is a preferable way to do it with char, as in the case of the original code).
What my codes does is pass two string and a count in the method countSubstring.
countSubString count the occurances of strTwo in strOne.
But I am have difficulty because I don't understand a few things:
public class CountingSubString
{
int CountSubString(String strOne, String strTwo, int count)
{
int i = 0;
int foundAtIndex = strOne.indexOf(strTwo, i);
if(foundAtIndex == -1)
{
i++;
}
else//(foundAtIndex != -1)
{
count++;
int newStartIndex = foundAtIndex + strTwo.length();
String StringFromString = strOne.substring(newStartIndex, strOne.length()-1);
count = count + countSubString(StringFromString, strTwo, count);
return count;
}
return count;
}
public class TestCountingSubString
{
public static void main(String[] argv)
{
String s2 = new String("abab");
String s3 = new String("ab");
String s4 = new String("aabbaa");
String s5 = new String("aa");
countingSubString CountOfString = new countingSubString();
int count = CountOfString.countSubString(s2, s3, 0);
System.out.println(count);
}
}
Question 1) let consider a case where string1 = c, and string2 = aa.
aa is not contained in c.
How do I make a base case for this case?
My attempt:
Question 2) In java how does a string end?
If I have string1 = "aabbaa", and string2 = "aa".
I get aa from index 0 and 1, so I return index 0. compute string2.length() + 0 = 2.
Now I substring string 1 at beginIndex: 2 to endindex: string2.length-1 to get new string to obtain "bbaa".
Searching again, I obtain string aa at index 2 and 3.
How do I make my recursive end after the string aa?
Why are you complicating things. It's java, use its features.
String string1 = "abab";
Pattern p = Pattern.compile("ab");
Matcher m = p.matcher(string1);
int count = 0;
while (m.find()){
count +=1;
}
System.out.println(count);
Also for your understanding, substring function has following format
public String substring(int beginIndex, int endIndex)
where
beginIndex -- the begin index, inclusive.
endIndex -- the end index, exclusive.
Safety condition as asked in Question 1
if (strOne == null || strOne.equals("") || strTwo.length() < sub.length())
return 0;
Solution to Question 2
int index = strOne.indexOf(strTwo);
if(index!=-1){
count++;
count+= countSubString(strOne.substring(index+1),strTwo,0);
}
So complete solution is
class countingSubString
{
int countSubString(String strOne, String strTwo, int count)
{
if (strOne == null || strOne.equals("") || strOne.length() < strTwo.length())
return 0;
int index = strOne.indexOf(strTwo);
if(index!=-1){
count++;
count+= countSubString(strOne.substring(index+1),strTwo,0);
}
return count;
}
}
Also remove public modifier from class countingSubString as there can be only one public class in one file. And also follow naming convention so class name should be
CountingSubString instead of countingSubString
You can use a recursive function as follows. I modified the class and function names a little bit.
You don't need to pass 'count' parameter to countSub function, since it will be eventually returned recursively.
public class Count
{
public static void main(String[] argv) {
String s2 = new String("ababab");
String s3 = new String("ab");
String s4 = new String("aabbaa");
String s5 = new String("aa");
int count = countSub(s2, s3);
System.out.println(count);
}
public static int countSub(String strOne, String strTwo) {
int foundAtIndex = strOne.indexOf(strTwo);
if(foundAtIndex == -1) {
return 0;
} else {
int newStartIndex = foundAtIndex + strTwo.length();
String newString = strOne.substring(newStartIndex, strOne.length());
return (1 + countSub(newString, strTwo));
}
}
}
Hi I've been doing this java program, i should input a string and output the longest palindrome that can be found ..
but my program only output the first letter of the longest palindrome .. i badly need your help .. thanks!
SHOULD BE:
INPUT : abcdcbbcdeedcba
OUTPUT : bcdeedcb
There are two palindrome strings : bcdcb and bcdeedcb
BUT WHEN I INPUT : abcdcbbcdeedcba
output : b
import javax.swing.JOptionPane;
public class Palindrome5
{ public static void main(String args[])
{ String word = JOptionPane.showInputDialog(null, "Input String : ", "INPUT", JOptionPane.QUESTION_MESSAGE);
String subword = "";
String revword = "";
String Out = "";
int size = word.length();
boolean c;
for(int x=0; x<size; x++)
{ for(int y=x+1; y<size-x; y++)
{ subword = word.substring(x,y);
c = comparisonOfreverseword(subword);
if(c==true)
{
Out = GetLongest(subword);
}
}
}
JOptionPane.showMessageDialog(null, "Longest Palindrome : " + Out, "OUTPUT", JOptionPane.PLAIN_MESSAGE);
}
public static boolean comparisonOfreverseword(String a)
{ String rev = "";
int tempo = a.length();
boolean z=false;
for(int i = tempo-1; i>=0; i--)
{
char let = a.charAt(i);
rev = rev + let;
}
if(a.equalsIgnoreCase(rev))
{
z=true;
}
return(z);
}
public static String GetLongest(String sWord)
{
int sLength = sWord.length();
String Lpalindrome = "";
int storage = 0;
if(storage<sLength)
{
storage = sLength;
Lpalindrome = sWord;
}
return(Lpalindrome);
}
}
modified program..this program will give the correct output
package pract1;
import javax.swing.JOptionPane;
public class Palindrome5
{
public static void main(String args[])
{
String word = JOptionPane.showInputDialog(null, "Input String : ", "INPUT", JOptionPane.QUESTION_MESSAGE);
String subword = "";
String revword = "";
String Out = "";
int size = word.length();
boolean c;
String Lpalindrome = "";
int storage=0;
String out="";
for(int x=0; x<size; x++)
{ for(int y=x+1; y<=size; y++)
{ subword = word.substring(x,y);
c = comparisonOfreverseword(subword);
if(c==true)
{
int sLength = subword.length();
if(storage<sLength)
{
storage = sLength;
Lpalindrome = subword;
out=Lpalindrome;
}
}
}
}
JOptionPane.showMessageDialog(null, "Longest Palindrome : " + out, "OUTPUT", JOptionPane.PLAIN_MESSAGE);
}
public static boolean comparisonOfreverseword(String a)
{ String rev = "";
int tempo = a.length();
boolean z=false;
for(int i = tempo-1; i>=0; i--)
{
char let = a.charAt(i);
rev = rev + let;
}
if(a.equalsIgnoreCase(rev))
{
z=true;
}
return(z);
}
}
You have two bugs:
1.
for(int y=x+1; y<size-x; y++)
should be
for(int y=x+1; y<size; y++)
since you still want to go all the way to the end of the string. With the previous loop, since x increases throughout the loop, your substring sizes decrease throughout the loop (by removing x characters from their end).
2.
You aren't storing the longest string you've found so far or its length. The code
int storage = 0;
if(storage<sLength) {
storage = sLength;
...
is saying 'if the new string is longer than zero characters, then I will assume it is the longest string found so far and return it as LPalindrome'. That's no help, since we may have previously found a longer palindrome.
If it were me, I would make a static variable (e.g. longestSoFar) to hold the longest palindrome found so far (initially empty). With each new palindrome, check if the new one is longer than longestSoFar. If it is longer, assign it to longestSoFar. Then at the end, display longestSoFar.
In general, if you're having trouble 'remembering' something in the program (e.g. previously seen values) you have to consider storing something statically, since local variables are forgotten once their methods finish.
public class LongestPalindrome {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String S= "abcdcba";
printLongestPalindrome(S);
}
public static void printLongestPalindrome(String S)
{
int maxBack=-1;
int maxFront = -1;
int maxLength=0;
for (int potentialCenter = 0 ; potentialCenter < S.length();potentialCenter ++ )
{
int back = potentialCenter-1;
int front = potentialCenter + 1;
int longestPalindrome = 0;
while(back >=0 && front<S.length() && S.charAt(back)==S.charAt(front))
{
back--;
front++;
longestPalindrome++;
}
if (longestPalindrome > maxLength)
{
maxLength = longestPalindrome+1;
maxBack = back + 1;
maxFront = front;
}
back = potentialCenter;
front = potentialCenter + 1;
longestPalindrome=0;
while(back >=0 && front<S.length() && S.charAt(back)==S.charAt(front))
{
back--;
front++;
longestPalindrome++;
}
if (longestPalindrome > maxLength)
{
maxLength = longestPalindrome;
maxBack = back + 1;
maxFront = front;
}
}
if (maxLength == 0) System.out.println("There is no Palindrome in the given String");
else{
System.out.println("The Longest Palindrome is " + S.substring(maxBack,maxFront) + "of " + maxLength);
}
}
}
I have my own way to get longest palindrome in a random word. check this out
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println(longestPalSubstr(in.nextLine().toLowerCase()));
}
static String longestPalSubstr(String str) {
char [] input = str.toCharArray();
Set<CharSequence> out = new HashSet<CharSequence>();
int n1 = str.length()-1;
for(int a=0;a<=n1;a++)
{
for(int m=n1;m>a;m--)
{
if(input[a]==input[m])
{
String nw = "",nw2="";
for (int y=a;y<=m;y++)
{
nw=nw+input[y];
}
for (int t=m;t>=a;t--)
{
nw2=nw2+input[t];
}
if(nw2.equals(nw))
{
out.add(nw);
break;
}
}
}
}
int a = out.size();
int maxpos=0;
int max=0;
Object [] s = out.toArray();
for(int q=0;q<a;q++)
{
if(max<s[q].toString().length())
{
max=s[q].toString().length();
maxpos=q;
}
}
String output = "longest palindrome is : "+s[maxpos].toString()+" and the lengths is : "+ max;
return output;
}
this method will return the max length palindrome and the length of it. its a way that i tried and got the answer. and this method will run whether its a odd length or even length.
public class LongestPalindrome {
public static void main(String[] args) {
HashMap<String, Integer> result = findLongestPalindrome("ayrgabcdeedcbaghihg123444456776");
result.forEach((k, v) -> System.out.println("String:" + k + " Value:" + v));
}
private static HashMap<String, Integer> findLongestPalindrome(String str) {
int i = 0;
HashMap<String, Integer> map = new HashMap<String, Integer>();
while (i < str.length()) {
String alpha = String.valueOf(str.charAt(i));
if (str.indexOf(str.charAt(i)) != str.lastIndexOf(str.charAt(i))) {
String pali = str.substring(i, str.lastIndexOf(str.charAt(i)) + 1);
if (isPalindrome(pali)) {
map.put(pali, pali.length());
i = str.lastIndexOf(str.charAt(i));
}
}
i++;
}
return map;
}
public static boolean isPalindrome(String input) {
for (int i = 0; i <= input.length() / 2; i++) {
if (input.charAt(i) != input.charAt(input.length() - 1 - i)) {
return false;
}
}
return true;
}
}
This approach is simple.
Output:
String:abcdeedcba Value:10
String:4444 Value:4
String:6776 Value:4
String:ghihg Value:5
This is my own way to get longest palindrome. this will return the length and the palindrome word
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println(longestPalSubstr(in.nextLine().toLowerCase()));
}
static String longestPalSubstr(String str) {
char [] input = str.toCharArray();
Set<CharSequence> out = new HashSet<CharSequence>();
int n1 = str.length()-1;
for(int a=0;a<=n1;a++)
{
for(int m=n1;m>a;m--)
{
if(input[a]==input[m])
{
String nw = "",nw2="";
for (int y=a;y<=m;y++)
{
nw=nw+input[y];
}
for (int t=m;t>=a;t--)
{
nw2=nw2+input[t];
}
if(nw2.equals(nw))
{
out.add(nw);
break;
}
}
}
}
int a = out.size();
int maxpos=0;
int max=0;
Object [] s = out.toArray();
for(int q=0;q<a;q++)
{
if(max<s[q].toString().length())
{
max=s[q].toString().length();
maxpos=q;
}
}
String output = "longest palindrome is : "+s[maxpos].toString()+" and the lengths is : "+ max;
return output;
}
this method will return the max length palindrome and the length of it. its a way that i tried and got the answer. and this method will run whether its a odd length or even length.
Write a Java function such that Given two strings, word and a separator, return a big string made of count occurences of the word, separated by the separator string.
repeatSeparator("Word", "X", 3) → "WordXWordXWord"
repeatSeparator("This", "And", 2) → "ThisAndThis"
repeatSeparator("This", "And", 1) → "This"
My code is as below but is not working
public String repeatSeparator(String word, String sep, int count) {
if(count == 1) {
return word;
}
if(count > 1) {
for (int i = 0; i < count-1; i++){
word = word + sep + word;
}
}
return word;
}
Example Output ::
Expected Run Result
repeatSeparator("Word", "X", 3) → "WordXWordXWord" "WordXWordXWordXWord" X
word = word + sep + word;
Think carefully about what this does the second time around. Hint: word has changed since the first time around.
Solution: Use a different variable to hold the results, so that you append the same word each time. (Free hint: use a StringBuffer or StringBuilder instead.)
The below function should do what you need:
public String repeatSeparator(String word, String sep, int count) {
StringBuffer buffer = new StringBuffer();
while (count > 0) {
buffer.append(word);
count--;
if (count > 0) {
buffer.append(sep);
}
}
return buffer.toString();
}
public string doStuff(
final String word,
final String seperator,
final int count)
{
StringBuffer buffer = new StringBuffer();
for (int index = 0; index < count; ++index)
{
if (buffer.length() > 0)
{
buffer.append(seperator);
}
buffer.append(word);
}
return buffer.toString();
}
public String repeatSeparator(String word, String sep, int count) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < count; i++) {
sb.append(word);
if (i < count - 1)
sb.append(sep);
}
return sb.toString();
}
In real life, would use Apache Common Lang StringUtils.repeat
Don't for get to consider the edge cases too. If the count is zero then the program should return an empty String.
public String repeatSeparator(String word, String sep, int count) {
String ans ="";
for(int i=0;i<count;i++)
{
if(i<count-1)
{
ans+=word + sep;
} else {
ans+=word;
}
}
return ans;
}
public String repeatSeparator(String word, String sep, int count) {
String s = "";
if (count > 1)
{
while (count > 0)
{
if (count > 1)
{
s = s + word + sep;
count--;
}
if (count == 1)
{
s = s + word;
count--;
}
}
}
if (count == 1) s = word;
return s;
}
public static String repeatSeparator1(String word,String sep,int count){
StringBuilder sb = new StringBuilder();
for(int i=0;i<count;i++){
sb.append(word+sep);
}
return sb.substring(0, sb.lastIndexOf(sep));
}
used this:
public String repeatSeparator(String word, String sep, int count) {
String result = "";
if(count == 0)
return "";
for(int i = 0 ; i < count-1 ; i++){
result = result +(word+sep);
}
return result+word;
}
public String createSeparatorString(String word, String separator, int count) {
StringBuffer stringBuffer = new StringBuffer();
while (count > 0) {
stringBuffer.append(word);
count--;
if (count > 0) {
stringBuffer.append(separator);
}
}
return stringBuffer.toString();
}
Here's another similar approach(I append the word at the end of every string if the count is greater than 0):
public String repeatSeparator(String word, String sep, int count) {
String out="";
if(count>0){
for(int i=1; i<=count-1; i++){
out += word+sep;
}
return out+word;
}
else return out;
}
public String repeatSeparator(String word, String sep, int count) {
String g="";
for(int i=0;i<count;i++){
g=g+word;
if(i<count-1){
g=g+sep;
}
}
return g;
}
public static void main(String[] args) {
String str1="Word";
String str2="X";
int x=2;
System.out.println(repeatString(str1, str2, x));
}
public static String repeatString(String s1, String s2, int y) {
String dummy="";
for(int i=1; i<=y; i++) {
dummy+=s1;
if(i!=y) {
dummy+=s2;
}
}
return dummy;
}