Java program to capitalize the first and last letter of each word of a string

Capitalize the first and last letter of each word of a string

In this java program, we’re going to capitalize all the first and last character of the words in a string, Take a string input from the user and store it in the variable called “s”. Take a new variable “newstr” and initialize it with an empty string. Split the string into words and store it in the String array using regex. Take a for-each loop and store the first character by using subString() and capitalize that first character using upperCase() method and store it in the “firstchar” and rest of the string from 1 to length-1 store in “restchar” , get the last character of string using charAt() method and convert that character to string using toString() and then add the “firstchar” and “restchar” string in “newstr”

java program to capitalize the first and last letter of each word of a string

Algorithm

  • Take a string input from the user and store it in the variable called “s”.
  • Take a “newstr” variable initialize with an empty string.
  • Split the string into words and store it in the String array using regex.
  • Take a for-each loop and store the first character by using subString() and store it in the “firstchar” and rest of the string store in “restchar” .
  • Then add the “firstchar” and “restchar” string in “newstr”.

Code in Java

import java.util.Scanner;
public class CapitalizeTheFirstAndLastLetterOfString {
   public static void main(String[] args) {
     Scanner sc =new Scanner(System.in);
     System.out.print("Enter String : "); 
     String s = sc.nextLine();
     String newstr = "";

     String[] str = s.split("\\s"); // splitting sentence into word converted to String array

    for (String string : str) {     
      int length = string.length();
      String firstchar = string.substring(0, 1);
      String restchar = string.substring(1, length - 1);
      String lastchar = Character.toString(string.charAt(length - 1));
      newstr = newstr+firstchar.toUpperCase()+restchar+" ";
    } 
    System.out.println(newstr); 
  }
}
This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student)

Output

Enter String : prep inst is best
PreP InstA IS BesT

2 comments on “Java program to capitalize the first and last letter of each word of a string”


  • Souhardya

    Scanner sc=new Scanner(System.in); String str=sc.nextLine(); String [] arr = str.split(” “); String res=””;
    res+=Character.toUpperCase(str.charAt(0));
    for(int i=1;i<str.length()-1;i++){
    if(str.charAt(i+1)==' '|| str.charAt(i-1)==' ') {
    res+=Character.toUpperCase(str.charAt(i));
    }
    else
    res+=str.charAt(i);
    }
    res+=Character.toUpperCase(str.charAt(str.length()-1));
    System.out.println(res);


  • chetan

    String s = sc.nextLine();

    String newstr = “”;

    String[] str = s.split(” “); // splitting sentence into word converted to String array

    for (int i=0; i<str.length;i++) {

    newstr = newstr + Character.toUpperCase(str[i].charAt(0));

    for(int j=1; j<str[i].length()-1;j++)

    newstr = newstr + str[i].charAt(j);
    newstr = newstr + Character.toUpperCase(str[i].charAt(str[i].length()-1))+" ";

    }System.out.println(newstr);