0

I want to search sub string in given STRING without using any String API . I have tried with Hash Map but not able to get desired output . In this example i have done code for this using Hash Map, which is as following .

Input = "Bread butter and bread"

Output = bread -> 2

import java.util.HashMap;
import java.util.Set;

public class WordCount
{
static void duplicateWords(String inputString)
{
    //Splitting inputString into words

    String[] words = inputString.split(" ");

    //Creating one HashMap with words as key and their count as value

    HashMap<String, Integer> wordCount = new HashMap<String, Integer>();

    //Checking each word

    for (String word : words)
    {
        //whether it is present in wordCount

        if(wordCount.containsKey(word.toLowerCase()))
        {
            //If it is present, incrementing it's count by 1

            wordCount.put(word.toLowerCase(),         wordCount.get(word.toLowerCase())+1);
        }
        else
        {
            //If it is not present, put that word into wordCount with 1 as it's value

            wordCount.put(word.toLowerCase(), 1);
        }
    }

    //Extracting all keys of wordCount

    Set<String> wordsInString = wordCount.keySet();

    //Iterating through all words in wordCount

    for (String word : wordsInString)
    {
        //if word count is greater than 1

        if(wordCount.get(word) > 1)
        {
            //Printing that word and it's count

            System.out.println(word+" : "+wordCount.get(word));
        }
    }}


public static void main(String[] args)
{
    duplicateWords("Bread butter and bread");

}
}

But it is not working as i have wanted because it might not be possible with Hash Map . It have done splitting of words from the start, to look out for those re-occurring words But this desired output i might not able to get with Hash Map so i have written one more logic for that which is as following .Still it is not working . Can you please help me with these codes ?. What i want is output like this .

Input = "BreadbutterbreadButter" Output = bread -> 2 , butter ->2

package string;
import java.util.Scanner;
public class SearchString {
        public static void main(String args[]) {
            Scanner scan = new Scanner(System.in);
            System.out.println("enter main string");
            String s = scan.nextLine();
            System.out.println("enter string to be searched");
            String f = scan.nextLine();
            s = s + " ";
            int l = s.length();
            char a;
            int c = 0;
            String s1 = "";
            for (int i = 0; i < l; i++) {
                a = s.charAt(i);
                if (a != ' ') {
                    s1 = s1 + a;
                } else {
                    if (s1.equalsIgnoreCase(f) == true) {
                        c++;
                    }
                    s1 = "";
                }
            }
            System.out.println("Frequency of the word " + f + " is " + c);
        }                                         
    }

Thanks & Regards,

PD

P.S : Please don't see my mistakes in this post . It is my first post here .

Edited :

i don't want any already developed algorithm please see my second code i want to do it by that approach .I think i'm very close to the answer as for second code i'm getting result like this

enter main string

Input >ram ramram

enter string to be searched

Output>ram

Frequency of the word ram is 1

DESIRED OUTPUT >but i want ram to be 3 .

  • To iterate over a hashmap do this: for (String word : words.keySet()) – mstorkson Jan 04 '17 at 15:29
  • Sorry, but your are actually providing too much (confusing) input here. You see; Maps **can** be used to solve such puzzles, and your first code parts look good. Thus: please turn here [help] and read about how to use [mcve] for example. In other words: "is not working" isn't a description we can help you with. Go for **one** case that doesn't work, put down the code for that; describe what goes in, and what you expect to come out. – GhostCat Jan 04 '17 at 15:30
  • 1
    Possible duplicate of [Fast algorithm for searching for substrings in a string](http://stackoverflow.com/questions/1765579/fast-algorithm-for-searching-for-substrings-in-a-string) – DimaSan Jan 04 '17 at 15:34
  • It will never work if you assume your words to be delimited by a space (as you did by using String.split(" ")). – João Neto Jan 04 '17 at 15:55
  • You'll need to search character-by-character and backtrack if it turns out to be a non-match. Another interesting edge case: If your input is "abababa" and your string to be searched is "aba", how many occurrences would you like it to count? Do intersecting occurrences count? I think the Knuth-Morris-Pratt is an interesting algorithm to take a look (sorry! I know you didn't want an already developed algorithm, but yours has flaws! :) ) – João Neto Jan 04 '17 at 16:00

0 Answers0