Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5837937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:24:52+00:00 2026-05-22T11:24:52+00:00

I am trying to implement a program that will take a users input, split

  • 0

I am trying to implement a program that will take a users input, split that string into tokens, and then search a dictionary for the words in that string. My goal for the parsed string is to have every single token be an English word.

For Example:

Input:
       aman

Split Method:
      a man
      a m an
      a m a n
      am an
      am a n
      ama n

Desired Output:
      a man

I currently have this code which does everything up until the desired output part:

    import java.util.Scanner;
import java.io.*;

public class Words {

    public static String[] dic = new String[80368];

    public static void split(String head, String in) {

        // head + " " + in is a segmentation 
        String segment = head + " " + in;

        // count number of dictionary words
        int count = 0;
        Scanner phraseScan = new Scanner(segment);
        while (phraseScan.hasNext()) {
            String word = phraseScan.next();
            for (int i=0; i<dic.length; i++) {
                if (word.equalsIgnoreCase(dic[i])) count++;
            }
        }

        System.out.println(segment + "\t" + count + " English words");

        // recursive calls
        for (int i=1; i<in.length(); i++) {
            split(head+" "+in.substring(0,i), in.substring(i,in.length()));
        }   
    }

    public static void main (String[] args) throws IOException {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = scan.next();
        System.out.println();

        Scanner filescan = new Scanner(new File("src:\\dictionary.txt"));
        int wc = 0;
        while (filescan.hasNext()) {
            dic[wc] = filescan.nextLine();
            wc++;
        }

        System.out.println(wc + " words stored");

        split("", input);

    }
}

I know there are better ways to store the dictionary (such as a binary search tree or a hash table), but I don’t know how to implement those anyway.

I am stuck on how to implement a method that would check the split string to see if every segment was a word in the dictionary.

Any help would be great,
Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:24:53+00:00Added an answer on May 22, 2026 at 11:24 am

    Splitting the input string every possible way is not going to finish in a reasonable amount of time if you want to support 20 or more characters. Here’s a more efficient approach, comments inline:

    public static void main(String[] args) throws IOException {
        // load the dictionary into a set for fast lookups
        Set<String> dictionary = new HashSet<String>();
        Scanner filescan = new Scanner(new File("dictionary.txt"));
        while (filescan.hasNext()) {
            dictionary.add(filescan.nextLine().toLowerCase());
        }
    
        // scan for input
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = scan.next().toLowerCase();
        System.out.println();
    
        // place to store list of results, each result is a list of strings
        List<List<String>> results = new ArrayList<>();
    
        long time = System.currentTimeMillis();
    
        // start the search, pass empty stack to represent words found so far
        search(input, dictionary, new Stack<String>(), results);
    
        time = System.currentTimeMillis() - time;
    
        // list the results found
        for (List<String> result : results) {
            for (String word : result) {
                System.out.print(word + " ");
            }
            System.out.println("(" + result.size() + " words)");
        }
        System.out.println();
        System.out.println("Took " + time + "ms");
    }
    
    public static void search(String input, Set<String> dictionary,
            Stack<String> words, List<List<String>> results) {
    
        for (int i = 0; i < input.length(); i++) {
            // take the first i characters of the input and see if it is a word
            String substring = input.substring(0, i + 1);
    
            if (dictionary.contains(substring)) {
                // the beginning of the input matches a word, store on stack
                words.push(substring);
    
                if (i == input.length() - 1) {
                    // there's no input left, copy the words stack to results
                    results.add(new ArrayList<String>(words));
                } else {
                    // there's more input left, search the remaining part
                    search(input.substring(i + 1), dictionary, words, results);
                }
    
                // pop the matched word back off so we can move onto the next i
                words.pop();
            }
        }
    }
    

    Example output:

    Enter a string: aman
    
    a man (2 words)
    am an (2 words)
    
    Took 0ms
    

    Here’s a much longer input:

    Enter a string: thequickbrownfoxjumpedoverthelazydog
    
    the quick brown fox jump ed over the lazy dog (10 words)
    the quick brown fox jump ed overt he lazy dog (10 words)
    the quick brown fox jumped over the lazy dog (9 words)
    the quick brown fox jumped overt he lazy dog (9 words)
    
    Took 1ms
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement simple program in Java that will be used to
I am trying to create a java program that will take in a directory
In a program that I'm trying to write now I take two columns of
I'm trying to write a little program that will add mDNS CNAME aliases to
I'm trying to implement a program in Xcode that's somewhat like a command line.
I am trying to implement string unescaping with Python regex and backreferences, and it
I'm trying to implement a system that works with faxes. We have a gatewary,
I'm trying to implement a program which runs a function limited by a fixed
I'm trying to write a graphical program in C++ with QT where users can
I am trying to implement a very basic before advice with Spring.Net, that just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.