without using built-in functions
Hai sir...
Please help me to solve this...
Findout occurances of each string in every another string
(built - in functions are not at all allowed)
all, and, sand, falling wall and sand
View Answers
October 8, 2012 at 5:01 PM
Here is a java example that finds the occurrences of each string in every another string.
class Occurrence
{
public static void main(String[] args)
{
String str = "all and sand falling wall and sand";
String findStr = "and";
int lastIndex = 0;
int count = 0;
while ((lastIndex = str.indexOf(findStr, lastIndex)) != -1) {
count++;
lastIndex += findStr.length() - 1;
}
System.out.println(findStr+" occurs " +count+ " times");
}
}
October 8, 2012 at 5:02 PM
Here is another code.
import java.util.*;
public class CountWordOccurrence {
public static void main(String[] args){
String[] st = {"all","and","sand","falling wall","and","sand"};
HashMap<String, Integer> map = new HashMap<String, Integer>();
String str="";
for(int i=0;i<st.length;i++){
str+=st[i]+" ";
}
str = str.toLowerCase();
int count = -1;
for (int i = 0; i < str.length(); i++) {
if ((!Character.isLetter(str.charAt(i))) || (i + 1 == str.length())) {
if (i - count > 1) {
if (Character.isLetter(str.charAt(i)))
i++;
String word = str.substring(count + 1, i);
if (map.containsKey(word)) {
map.put(word, map.get(word) + 1);
}
else {
map.put(word, 1);
}
}
count = i;
}
}
ArrayList<Integer> list = new ArrayList<Integer>();
list.addAll(map.values());
Collections.sort(list, Collections.reverseOrder());
int last = -1;
for (Integer i : list) {
if (last == i)
continue;
last = i;
for (String s : map.keySet()) {
if (map.get(s) == i)
System.out.println(s + ":" + i);
}
}
}
}
Ads
Related Tutorials/Questions & Answers:
without using built-in functions
without using built-in functions Hai sir...
Please help me to solve this...
Findout occurances of each string in every another string
(
built - in
functions are not at all allowed)
all, and, sand, falling wall and sand
Advertisements
Java reverse string without using inbuilt functions
Java reverse string
without using inbuilt
functions
In this tutorial, you will learn how to reverse string
without using any
inbuilt
functions. Here, we have created a method to reverse the string and
returns a new string with reverse
using tiles without struts
using tiles
without struts Hi
I am trying to make an application
using tiles 2.0.
Description of my web.xml is as follows:
tiles
org.apache.tiles.web.startup.TilesServlet
Sorting arraylist without using Collection.sort()
Sorting arraylist
without using Collection.sort() Hi,
How can I sort an arraylist
without using Collection.sort() nad also I am not allowed to use any other data structure in the program? Is there any algorithm by
using which I
Round of to a multiple of 5 without using function
Round of to a multiple of 5
without using function Please help me out...
U enter values like 239, 2543.876, 962....
Give me an equation
without using any function which will give output of 240, 2545, 960 respectively
Round of to a multiple of 5 without using function
Round of to a multiple of 5
without using function Please help me out...
U enter values like 239, 2543.876, 962....
Give me an equation
without using any function which will give output of 240, 2545, 960 respectively
greatest of 3 numbers using classes and functions.
greatest of 3 numbers
using classes and
functions. WAP to calculate greatest of 3 numbers
using classes and
functions with parameters through input in main?
Java Find Largest Number
import java.util.*;
class
String length without using length() method in java
String length
without using length() method in java How to count length of string
without using length() method in java?
Here is Example... the number of character into the given string
without using the length() method
Java repeat string without using loops
Java repeat string
without using loops
In this section, you will learn how to repeat string
without using for loops.
The given example accepts the number of times and iterates the string to the
number of times set by the user
without
Program using String functions - Java Beginners
Program
using String functions Write a program to accept a sentence and find out how many times the word "the" occurs? Hi Friend,
Try the following code:
import java.util.*;
class CountSameWords
without ;
without ; can u give me an example of a program
without
without ;
without ; can u give me an example of a program
without