Skip to main content
added 220 characters in body
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
/**
 * Transforms "StringsLikeThis" into "Strings like this".
 */
private static String separateWords(String interCaps) 
{
    StringBuilder s = new StringBuilder(interCaps); 
    for (int i = 1; i < s.length(); i++) {
        if (Character.isUpperCase(s.charAt(i))) {
            s.setCharAt(i, Character.toLowerCase(s.charAt(i)));
            s.insert(ii++, ' ');
        }
    }
    return s.toString();
}

If the sentence contains two consecutive spaces, the function crashes when attempting to take substrings from an empty word.

The one-argument form of String.substring(index) automatically takes everything to the end.

private static String toPigLatin(String sentence) {
    String[] words = sentence.split(" ");
    StringBuilder latin = new StringBuilder(sentence.length() + words.length * "ay".length());
    for (int i = 0; i < words.length; i++) {
        if (i > 0) {
            latin.append(' ');
        }
        String word = words[i];
        if (!word.isEmpty()) {
            char initial = words[i]word.charAt(0);
            char remaining = words[i]word.substring(1);
            latin.append(remaining).append(initial).append("ay");
        }
    }
    return latin.toString();
}
/**
 * Transforms "StringsLikeThis" into "Strings like this".
 */
private static String separateWords(String interCaps) 
{
    StringBuilder s = new StringBuilder(interCaps); 
    for (int i = 1; i < s.length(); i++) {
        if (Character.isUpperCase(s.charAt(i))) {
            s.setCharAt(i, Character.toLowerCase(s.charAt(i)));
            s.insert(i, ' ');
        }
    }
    return s.toString();
}

The one-argument form of String.substring(index) automatically takes everything to the end.

private static String toPigLatin(String sentence) {
    String[] words = sentence.split(" ");
    StringBuilder latin = new StringBuilder(sentence.length() + words.length * "ay".length());
    for (int i = 0; i < words.length; i++) {
        if (i > 0) {
            latin.append(' ');
        }
        char initial = words[i].charAt(0);
        char remaining = words[i].substring(1);
        latin.append(remaining).append(initial).append("ay");
    }
    return latin.toString();
}
/**
 * Transforms "StringsLikeThis" into "Strings like this".
 */
private static String separateWords(String interCaps) 
{
    StringBuilder s = new StringBuilder(interCaps); 
    for (int i = 1; i < s.length(); i++) {
        if (Character.isUpperCase(s.charAt(i))) {
            s.setCharAt(i, Character.toLowerCase(s.charAt(i)));
            s.insert(i++, ' ');
        }
    }
    return s.toString();
}

If the sentence contains two consecutive spaces, the function crashes when attempting to take substrings from an empty word.

The one-argument form of String.substring(index) automatically takes everything to the end.

private static String toPigLatin(String sentence) {
    String[] words = sentence.split(" ");
    StringBuilder latin = new StringBuilder(sentence.length() + words.length * "ay".length());
    for (int i = 0; i < words.length; i++) {
        if (i > 0) {
            latin.append(' ');
        }
        String word = words[i];
        if (!word.isEmpty()) {
            char initial = word.charAt(0);
            char remaining = word.substring(1);
            latin.append(remaining).append(initial).append("ay");
        }
    }
    return latin.toString();
}
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

separateWords()

Loops of this form…

int i = 1;
while (i < str.length())
{
    …
    i++;
}

… would be better written as a for loop.

The comments are inane. If you're going to write any comment at all, write JavaDoc.

/**
 * Transforms "StringsLikeThis" into "Strings like this".
 */
private static String separateWords(String interCaps) 
{
    StringBuilder s = new StringBuilder(interCaps); 
    for (int i = 1; i < s.length(); i++) {
        if (Character.isUpperCase(s.charAt(i))) {
            s.setCharAt(i, Character.toLowerCase(s.charAt(i)));
            s.insert(i, ' ');
        }
    }
    return s.toString();
}

toPigLatin()

The toPigLatin() function is worse than your separateWords() in two ways:

  • Code reusability: Since toPigLatin() prints its output instead of returning it, you'll never be able to reuse it for anything else. You can't feed its output into another string transformation function, like you did with the output of separateWords(). You won't be able to use it in a program with a graphical or web interface. You won't be able to run unit tests on the function.
  • Efficiency: When you do many string concatenations, use a StringBuilder. There is a lot of redundant copying here due to the use of String concatenation.

The output from this function has an extra space at the beginning and end. I would consider that a bug.

The one-argument form of String.substring(index) automatically takes everything to the end.

private static String toPigLatin(String sentence) {
    String[] words = sentence.split(" ");
    StringBuilder latin = new StringBuilder(sentence.length() + words.length * "ay".length());
    for (int i = 0; i < words.length; i++) {
        if (i > 0) {
            latin.append(' ');
        }
        char initial = words[i].charAt(0);
        char remaining = words[i].substring(1);
        latin.append(remaining).append(initial).append("ay");
    }
    return latin.toString();
}