In this blog, we will see how to capitalize the first character of each word present in a given string. There can be two different ways to achieve our desired result. Let us see both of them one after another.
1. Using Split Function with for Loop
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Scanner; public class capitalize { public static String char_capital (String input_str) { String words_string[] = input_str.split ("\\s"); String result_string = ""; for (int ind=0; ind < words_string.length ; ind++) { String single_word = words_string[ind]; String first = single_word.substring(0,1); String remaining = single_word.substring(1); result_string += first.toUpperCase()+ remaining + " "; } return result_string; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the input string: "); String input_string = sc.nextLine(); String result_string = char_capital(input_string); System.out.println("The string after capitalizing first character of each word is: "); System.out.println(result_string); } } |
Output:
1 2 3 4 |
Enter the input string: my name is pulkit govrani The string after capitalizing first character of each word is: My Name Is Pulkit Govrani |
Code Explanation:
We have created a function char_capital which takes input_string as an argument. Then the split method is used to split the string by space(“ ”) into an array of words. The for loop is initiated for the words_string array. The first character of each word is taken and converted into uppercase, and then we add the changed string to the result_string.
Inside the main function, we have created a scanner object to take a string as input from the user and then called the char_capital method for that string.
2. Using Only for Loop
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.util.Scanner; public class capitalize { public static String char_capital (String input_str) { String temp = ""; String result_string = ""; for(int ind=0;ind<input_str.length();ind++) { if(input_str.charAt(ind)==' ') { result_string += temp + " "; temp = ""; continue; } if(temp == "") { temp += Character.toUpperCase( input_str.charAt(ind)); }else { temp += input_str.charAt(ind); } } result_string += temp; return result_string; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the input string: "); String input_string = sc.nextLine(); String result_string = char_capital(input_string); System.out.println("The string after capitalizing first character of each word is: "); System.out.println(result_string); } } |
Output:
1 2 3 4 |
Enter the input string: hello user welcome to the java programmer The string after capitalizing first character of each word is: Hello User Welcome To The Java Programmer |
The logic for both programs are same, but they are implemented in their own different ways.
I hope you have understood the problem clearly. Please do comment below if you still have any doubts.