0

I am wondering how can i write a program that would randomly generate words and then randomly pick some of those words and replace random character of that words with *. * meaning wildcard which is replacment for any character from a-z. This txt file would then be used as and sample to test my main program which works fine at the moment. I would use this random list of words for testing how long program need using binary search and then normal search.

I dont need a code but an idea or an example how this could be done.

  • 8
    Welcome to Stack Overflow! It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive. – Avinash Raj Nov 18 '14 at 10:38
  • no i am asking for a idea or how to do it not to give me a code –  Nov 18 '14 at 10:50
  • I think if you more clearly defined what you want the program to do, it would be more apparent how you might code it. What do you mean it would randomly generate words? Should it make variable length blocks of random characters separated by space, or do you need a dictionary of real words to select from? I see the regex tag, but how do you think regex will help? Why do you say to replace letters with the * character only after words are picked, and not while you are picking the words? – le3th4x0rbot Nov 18 '14 at 10:57
  • You may be interested in Xeger. Check the accepted answer that is here: http://stackoverflow.com/questions/274011/random-text-generator-based-on-regex – kayleeFrye_onDeck Nov 18 '14 at 15:48

1 Answers1

0

you can create random words like this: use method Math.random() for random int values. cast these int values into a char value. example: char c = (char) 65; for that, you have to read about ASCII values. (65 casts to 'A')

i wrote a full example, which you can use.

public class Test {

    private static String createWord(int wordlength) {
        String word = "";
        for (int i = 0; i < wordlength; i++) {
            int ran = (int) (Math.random() * 2);
            if (ran == 0)
                word += (char) (int) (Math.random() * 26 + 'A');
            else
                word += (char) (int) (Math.random() * 26 + 'a');
        }
        return word;
    }

    public static void main(String[] args) {

        System.out.println(createWord(8));
    }
}

this answer is just about creating random words.

if you want to exchange random chars in a word/String, you can take a look at String's replace(oldChar, newChar) method in combination with creating random chars with Math.random().

EDIT: Do you want to change a char into a wildcard (word -> wo.d), or into some random letter (word -> wKrd)?

for the second one you can use following method:

private static String replaceCharInWord(String word) {
    int random = (int) (Math.random() * word.length());
    System.out.println(random);
    int ran = (int) (Math.random() * 2);
    char newChar = ' ';
    if (ran == 0)
        newChar = (char) (int) (Math.random() * 26 + 'A');
    else
        newChar = (char) (int) (Math.random() * 26 + 'a');
    return word.substring(0, random) + newChar + word.substring(random + 1);
}
huidube
  • 390
  • 3
  • 15