1

I am having some trouble with figuring out how to work on making a palindrome generator from random letters. e.g. asdffdsa, kiutgtuik. I need to have the size be entered and use that to make the palindrome. I have some code already, and was just curious if you could help point me in the right direction. I would also like to try and not use arrays either for this.

import java.util.Random;

public class PP425 {
    public static String generatePalindrome (int size) {
        Random rand = new Random();
        char ch = (char)(rand.nextInt(26) + 97);
        String random = "";
        for (int i = 0; i < size; i++) {

        }


        return random;
    }
    public static void main(String[] args) {
        System.out.println(generatePalindrome(3));
    }
}
Trying
  • 14,004
  • 9
  • 70
  • 110
itsaferbie
  • 81
  • 4
  • 9

3 Answers3

1

Create a char[] of size K. Generate random() number between 0 to 25 and add 'a'. Now what ever char you generate just place it in begin and end and increase begin, decrement end. Do this till begin <= end.

public static String fun(int k){
        long seed = System.currentTimeMillis();
        Random random1 = new Random(seed);
        char[] a = new char[k];
        int begin=0, end=k-1;
        while(begin<=end){
            char c = (char)(random1.nextInt(26)+'a');
            a[begin]=c;
            a[end]=c;
            begin++;end--;
        }
        return String.valueOf(a);
    }
Trying
  • 14,004
  • 9
  • 70
  • 110
0

Is this what you want? If for long size, use StringBuilder instead of String.

import java.util.Random;

public class PP425 {
    public static String generatePalindrome (int size) {
        Random rand = new Random();
        StringBuilder random = new StringBuilder(size);
        for (int i = 0; i < (int)Math.ceil((double)size/2); i++) {
            char ch = (char)(rand.nextInt(26) + 97);
            random.append(ch);
        }
        for(int i = size/2-1; i >= 0; i--)
            random.append(random.charAt(i));

        return random.toString();
    }
    public static void main(String[] args) {
        System.out.println(generatePalindrome(3));
    }
}    
knordbo
  • 552
  • 3
  • 7
  • 1
    Use `StringBuilder` *always* when you are constructing `String`s. The GC will thank you. – Darkhogg Nov 13 '13 at 23:47
  • @Darkhogg Yup, I just didn't want to edit too much of his code at first. But I always use StringBuilder myself while appending strings :) – knordbo Nov 13 '13 at 23:51
  • Almost, just needs to have it print out the number that is input for size, instead of double that number. – itsaferbie Nov 13 '13 at 23:52
  • @itsaferbie Sorry for that. Edited it! – knordbo Nov 13 '13 at 23:58
  • @ExceptionalException Not a problem. Thank you for the help! – itsaferbie Nov 14 '13 at 00:02
  • 1
    (yea, its more review/optimization) If you knew the size of the String you're going to build with StringBuilder, consider passing that in to the constructor to avoid allocating too much (or too little) space. The too little is the primary concern for when it has to reallocate the internal array and increase its size. Thus `new StringBuilder(2 * size)` is more optimal. –  Nov 14 '13 at 00:08
  • 1
    @MichaelT Clever detail. Edited it. – knordbo Nov 14 '13 at 00:15
  • For something of size 2, 3, 10... not a big deal (the default size is 16). But when you add the 17th character, it allocates a 32 character array and copies the 16 characters. And then 64... 128... This gets more and more expensive as it gets bigger. http://stackoverflow.com/questions/8953418/ - glance at [expandCapacity()](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/AbstractStringBuilder.java#AbstractStringBuilder.expandCapacity%28int%29) to see what its doing and consider the implications of a 10k long string starting with a default 16 char array –  Nov 14 '13 at 00:42
0

The call to random.nextInt() should not be placed outside the for loop, but inside of it. You don't necessarily need to use arrays. You can simply concatenate Strings (or use StringBuilder instead).

In the for loop you'd build the first half of the palindrome. Then, you can use the StringBuilder.reverse method in order to generate the second half.

Be aware of the middle letter, if your size is an odd number!

Also, your for loop should only run to the half of the size. Or (size - 1) / 2, if the size is an odd number.

Andrei Nicusan
  • 4,555
  • 1
  • 23
  • 36