java

capitalizeWords()

Parameters: String inputString

A string where each word to be capitalized is separated by space

Returns: A string where the first letter of each word is capitalized

This function receives a string as input and returns a new string where the first character of each word is capitalized. It uses Java's String methods to manipulate the input string.

Strings
Loops
Medium dificulty

Designing a Java Function to Capitalize Words

Hello dear programmer, you're welcome to this blog post. We'll be walking you through how to code a specific function in Java, 'capitalize-words'. This function capitalizes the initial letter of every word in a string. It's going to be a calm, friendly and educative journey. No slang, no jargon, just simple programming. Enjoy your read!

Step 1

Begin by defining a function that will take a string as an input. This is the basis of our solution.

public class Main {
  public static String capitalizeWords(String str) {
    // We will add our logic here
  }
}

Step 2

Split the input string into individual words. We will use the split method for this. This method separates a String into an array of substrings given a specific delimiter. In our case, the delimiter is a space character.

public class Main {
  public static String capitalizeWords(String str) {
    String[] words = str.split(" ");
  }
}

Step 3

Iterate over the array of words and capitalize the first character of each word. For this, we will use the substring method to separate the first character and the rest of the word. We will then convert the first character to uppercase using the toUpperCase method.

public class Main {
  public static String capitalizeWords(String str) {
    String[] words = str.split(" ");

    for (int i = 0; i < words.length; i++) {
      words[i] = words[i].substring(0, 1).toUpperCase() + words[i].substring(1);
    }
  }
}

Step 4

Now, we need to join the array of words back into a single string. We will use the String.join method for this.

public class Main {
  public static String capitalizeWords(String str) {
    String[] words = str.split(" ");

    for (int i = 0; i < words.length; i++) {
      words[i] = words[i].substring(0, 1).toUpperCase() + words[i].substring(1);
    }
    return String.join(" ", words);
  }
}

Step 5

Our function is now complete. Let's add a main method to test it with an example.

public class Main {
  public static String capitalizeWords(String str) {
    String[] words = str.split(" ");

    for (int i = 0; i < words.length; i++) {
      words[i] = words[i].substring(0, 1).toUpperCase() + words[i].substring(1);
    }
    return String.join(" ", words);
  }

  public static void main(String[] args) {
    System.out.println(capitalizeWords("hello world"));  // Outputs: Hello World
  }
}

The final solution works by splitting the string into individual words, capitalizing the first character of each word, and then joining the words back together.

Learn function in:

Word Capitalization

This function capitalizes the first letter of each word in a string.

Learn more

Mathematical principle

This function operates on the principle of string manipulation. It loops through the string, checks if a character is the first character after a whitespace or is the first character in the string. If it is, it converts that character to uppercase. Otherwise, it keeps the character as it is. In mathematical terms, the function performs N operations where N is the length of the string, thus it runs in O(N) time.

Learn more