开发者

Wind blowing on String

I have some basic idea on how to do this task, but I'm 开发者_如何学运维not sure if I'm doing it right. So we have class WindyString with metod blow. After using it :

System.out.println(WindyString.blow(
    "Abrakadabra!          The    second  chance to pass has already BEGUN! "));

we should obtain something like this :

                    e              a  e         a           a  ea y
   br k d br !    Th    s c nd   ch nc    t    p ss   h s    lr  d    B G N!
  A  a a a  a            e o               o           a               E U

so in a nutshell in every second word we pick every vowels and move them one line above. In the second half of words we move vowels one line below.

I know I should split string to tokens with tokenizer or split method,but what next ? Create 3 arrays each representing each row ?


Yes, that's probably an easy (not very performant) way to solve the problem.

Create 3 arrays; one is filled with the actual data and 2 arrays are filled (Arrays.fill) with ' '.

Then iterate over the array containing the actual data, and keep an integer of which word you're currently at and a boolean if you already matched whitespace.

While iterating, you check if the character is a vowel or not. If it's a vowel, check the word-count (oddness/evenness) and place it in the first or third array. When you reach a whitespace, set the boolean and increase the word count. If you reach another whitespace, check whether the whitespace is already set: if so, continue. If you match a non-whitespace, reset the whitespace boolean.

Then join all arrays together and append a new-line character between each joined array and return the string.


The simplest way is to use regex. This should be instructive:

static String blow(String s) {
    String vowels = "aeiouAEIOU";
    String middle = s.replaceAll("[" + vowels + "]", " ");
    int flip = 0;
    String[] side = { "", "" };
    Scanner sc = new Scanner(s);
    for (String word; (word = sc.findInLine("\\s*\\S*")) != null; ) {
        side[flip] += word.replaceAll(".", " ");
        side[1-flip] += word.replaceAll("[^" + vowels + "]", " ");
        flip = 1-flip;
    }
    return String.format("|%s|%n|%s|%n|%s|", side[0], middle, side[1]);
}

I added the | characters in the output to show that this processes excess whitespaces correctly -- all three lines are guaranteed the same length, taking care of leading blanks, trailing blanks, or even ALL blanks input.

If you're not familiar with regular expressions, this is definitely a good one to start learning with.

The middle is simply the original string with all vowels replaced with spaces.

Then, side[0] and side[1] are the top and bottom lines respectively. We use the Scanner to extract every word (preserving leading and trailing spaces). The way we process each word is that in one side, everything is replaced by blanks; in the other, only non-vowels are replaced by blanks. We flip sides with every word we process.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜