anna
anna

Reputation: 21

Repeated Characters in Array

How do you line up two char arrays with as many repeated characters as possible? This is what I got.

public class Repeat {

    public static void main(String[] args) {

        String Foo = "HELLO";
        String Boo = "VEX";

        char [] Key = new char[Foo.length()];

        for(int i = 0; i < Foo.length(); i++) {
            for(int j = 0; j < Key.length; j++) {
                if(i < Boo.length()) {
                    Key[i] = Boo.charAt(i);

                }

            }
        }

        for(char c : Key) {
            System.out.print(c + " ");
        }
    }

}

It currently prints out V E X [ ] [ ]

I want the Key to print out

V E X V E 

so it lines up with

H E L L O

Upvotes: 2

Views: 680

Answers (3)

mezzodrinker
mezzodrinker

Reputation: 988

I have no idea why one might want to do something like that, but you can use the modulo operator % together with both the shorter and the longer string's length to achieve what you want to do. Something like this:

String longString = "abcde";
String shortString = "abc";
char[] array = new char[longString.length()];

for(int i = 0; i < array.length(); i++) {
    array[i] = shortString.charAt(i % shortString.length());
}

If you then output the contents of array, you should get abcab.

How that code works

The for loop iterates from 0 to the length of array (which equals longString.length()) less 1. This assures that all elements of the array (0, 1, ... , array.length() - 1) can be initialized in the loop's body.

The statement i % shortString.length() determines the position of the character that should be retrieved from shortString. Since i may be greater than or equal to shortString.length() (which would cause an exception if used as parameter for shortString.charAt(int)), you have to add the modulo operator which makes sure that the parameter of shortString.charAt(int) stays within the range from 0 (inclusive) to shortString.length() (exclusive) but causes another character to be returned in every iteration.

Upvotes: 2

Andreas
Andreas

Reputation: 159086

Here is a method for doing that repeated printing for unlimited number of strings:

private static void printRepeatedToLongest(String ... values) {
    int maxLen = 0;
    for (String value : values)
        if (value.length() > maxLen)
            maxLen = value.length();
    for (String value : values)
        if (value.length() == maxLen)
            System.out.println(value);
        else {
            char[] buf = new char[maxLen];
            for (int i = 0, len; i < maxLen; i += len) {
                len = Math.min(value.length(), maxLen - i);
                value.getChars(0, len, buf, i);
            }
            System.out.println(buf);
        }
}

Test

public static void main(String[] args) {
    printRepeatedToLongest();       // Prints nothing
    printRepeatedToLongest("");     // Prints a blank line
    printRepeatedToLongest("FOO");  // Prints the string
    printRepeatedToLongest("HELLO", "VEX");
    printRepeatedToLongest("recondite", "injure", "earthy", "taste", "calm", "minute",
                           "jealous", "level", "intend", "infamous", "men", "defeated");
}

Output

           <-- blank line
FOO
HELLO
VEXVE
recondite
injureinj
earthyear
tastetast
calmcalmc
minutemin
jealousje
levelleve
intendint
infamousi
menmenmen
defeatedd

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521053

Here is a solution which allows you to build the output String without using an explicit loop:

public static void main(String[] args) {
    String foo = "HELLO";
    String boo = "VEX";
    String repeated = new String(new char[foo.length() / boo.length()]).replace("\0", boo);
    repeated += boo.substring(0, foo.length() % boo.length());
    repeated = repeated.replace("", " ").trim();

    System.out.println(repeated);
}

Output:

V E X V E 

Upvotes: 1

Related Questions