Capitalize last letter and Lowercase first letter of a word in Java


String is a sequence of character values. In Java, Strings are considered as objects. We have a String class provided by Java for creating and manipulating strings.

We have to convert the first letter of the word to lowercase and last letter of the word to uppercase.

In this article we will see how the first and last letter can be converted into lower and upper case respectively. Let’s explore.

To Show You Some Instances

Instance-1

Suppose the input string is “Hello”

After converting the first letter to lower and last letter to capital, the new string will be “hellO”

Instance-2

Suppose the input string is “Java”

After converting the first letter to lower and last letter to capital, the new string will be “javA”

Instance-3

Suppose the input string is “Programming”

After converting the first letter to lower and last letter to capital, the new string will be “programminG”

Syntax

To get the length of the String Java String class provides a length() method.

Below is the syntax for that −

str.length();

Where, str is the string variable.

To get a substring of the original string Java String class provides a substring() method.

Syntax

Below is the syntax for that −

str.substring(int startIndex);
str.substring(int startIndex, int endIndex);

Where, startIndex is inclusive and endIndex is exclusive.

To get a character at a specified index Java String class provides the charAt() method.

Syntax

Below is the syntax for that −

str.charAt(int index)

Where, index refers to the index of the character that you need.

To convert different types of values to String value Java String class provides valueOf() method.

Syntax

Below is the syntax for that −

String.valueOf(Object obj)

Algorithm

Note − This problem can be solved with the Array concept also. But here we have tried to solve the problem without using the array concept. Also we have used only inbuilt methods of String Class.

Algorithm-1

  • Step 1 − Get the string/word either by initialization or by user input.

  • Step 2 − Get the first and last letter by using the substring() method. Then convert it to lower and upper case by using toLowerCase() method and toUpperCase() method respectively.

  • Step 3 − Get the middle characters (except first and last character) with the help of substring() method.

  • Step 4 − Combine first, middle and last value and get the final string/word.

Algorithm-2

  • Step 1 − Get the string/word either by initialization or by user input.

  • Step 2 − Get the first and last letter by using charAt() method. Then convert it to lower and upper case by using toLowerCase() method and toUpperCase() method respectively.

  • Step 3 − Get the middle characters (except first and last character) with the help of charAt() method and a for loop.

  • Step 4 − Then combine first, middle and last value and get the final string/word.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using inbuilt substring() Method

  • By Using inbuilt charAt() Method

Let’s see the program along with its output one by one.

Approach-1: By Using Inbuilt substring Method

Example

In this approach, we will make use of Algorithm-1

import java.util.Scanner;
public class Main{
   public static void main(String[] args) {
   
      //input string
      String str = "Java";
      System.out.println("Original string is: "+str);
      
      //get size of the string 
      int size = str.length();
      
      //get last character and convert it to upper case
      String last = str.substring(size-1,size);
      String lastinUpper = last.toUpperCase();
      
      //get first character and convert it to lower case
      String first = str.substring(0,1);
      String firstinLower = first.toLowerCase();
      
      //get middle parts of the word, except first and last character
      String middle = str.substring(1,size-1);
      
      //combine everything and get the final string
      String result = firstinLower+middle+lastinUpper;
      
      //print result
      System.out.println("Updated string is: "+result);
   }
}

Output

Original string is: Java
Updated string is: javA

Approach-2: By Using Inbuilt charAt() Method

Example

In this approach, we will make use of Algorithm-2

public class Main{
   public static void main(String[] args) { 
   
      //input String
      String str = "Python";
      System.out.println("Original string: "+str);
      
      //get length of string
      int size = str.length();
      
      //find last character and convert it to upper case
      char last = str.charAt(size-1);
      String finalLast = (String.valueOf(last)).toUpperCase();
      
      //find first character and convert it to lowercase
      char first = str.charAt(0);
      String finalFirst = (String.valueOf(first)).toLowerCase();
      
      //find middle characters
      String middle="";
      for(int i=1; i<size-1; i++){
         String s = String.valueOf(str.charAt(i));
         middle=middle+s;
      }
      
      //find the updated string 
      String result = finalFirst+middle+finalLast;
      System.out.println("Updated string: "+result);
   }
}

Output

Original string: Python
Updated string: pythoN

In this article, we explored how to convert the first letter of a word to lowercase and last letter of the word to upper case in Java by using different approaches.

Updated on: 05-Jan-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements