Shuffle middle characters of words in a string?

Here is the question I need to solve:

I  dn’ot gvie a dman for a man taht can only sepll a word one way. 

      

Write a String scramble method that builds a scrambled version of a given word by randomly flipping two characters apart from the first and last. Then write a program that reads the words from the file "input.txt" (one word per line) and outputs each word with its scrambled version (one pair per line) to another file "scrambled.txt".

I don't need a string to convert to words, just one word per line. I need to read words, not lines.

So far I have done this:

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

public class examhelp
{
    public static void main(String[]args)throws IOException
    {
        Scanner kbd=new Scanner(System.in);
        File f=new File("words.txt");
        Scanner inputFile=new Scanner(f);
        PrintWriter outputFile=new PrintWriter("scrambled.txt");
        bloop wlist=new bloop();
        while(inputFile.hasNext())
        {
            String ww=inputFile.nextLine();
            System.out.println(ww);
            outputFile.println(wlist.scramble(ww));
        }
        inputFile.close();
        outputFile.close();
    }
}

class bloop
{
    public static String scramble(String word)
    {
        String shuffledString = ""; 

        while (word.length() != 0)
        {
            int index = (int) Math.floor(Math.random() * word.length());
            char c = word.charAt(index);
            word = word.substring(0,index)+word.substring(index+1);
            shuffledString += c;
        }

        return shuffledString;
    }
}

      

Now the problem is that it shuffles ALL letters including the first and last, and I can't seem to do that as per the question. Could you please help me with my method and what kind of code should be there? I can look at it and see where I was wrong and what I need to do. Thank.

I need to do this WITHOUT arrays or any predefined methods.

+3


source to share


2 answers


Start by using an existing method scramble

and go for it private

; we will also change it to take two additional arguments (both types char

) like

private static String scramble(char first, char last, String word)
{
    String shuffledString = "" + first; // <-- add the first char
    while (word.length() != 0)
    {
        int index = (int) Math.floor(Math.random() * word.length());
        char c = word.charAt(index);
        word = word.substring(0,index)+word.substring(index+1);
        shuffledString += c;
    }
    return shuffledString + last; // <-- add the last char
}

      

We can then use this method to implement a version public

that crosses the middle as

public static String scramble(String word) {
    if (word.length() < 3) {
        return word;
    }
    String middle = word.substring(1, word.length() - 1);
    return scramble(word.charAt(0), word.charAt(word.length() - 1), middle);
}

      

Edit Also as mentioned below; you're using



String ww=inputFile.nextLine();

      

but your loop is on Scanner.hasNext()

. If you change this toScanner.next()

String ww=inputFile.next();

      

instead of line (s), you should use selected white space markers.

+1


source


Step 1: Hopefully you can understand that the candidate's words to be shuffled must be at least 4 characters long; you omit the lesser words, just returning them as they are.

Step 2: Pick a random index other than the first and last.

Step 3: Flip this randomly selected symbol with one in each on the right.



Performance Tip 1: You can also improve performance slightly by dropping the semi-final index. No need to select and flip the semi-final symbol as the final (which is omitted) is in every right.

Performance hint 2: or you can omit the second index from the random selection (index = 1) and always switch with the one on the left, can you see why?

This implementation is pretty straightforward, but I'll leave it to you as that's the purpose.

0


source







All Articles