Home > Java > javaTutorial > In Java, capitalize the last letter of a word and lowercase the first letter

In Java, capitalize the last letter of a word and lowercase the first letter

PHPz
Release: 2023-08-20 10:05:07
forward
759 people have browsed it

In Java, capitalize the last letter of a word and lowercase the first letter

String is a sequence of character values. In Java, String is treated as an object. 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.

Show you some examples

The Chinese translation of

Instance-1

is:

Instance-1

Assume the input string is "Hello"

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

The translation of

Instance-2

is:

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 lowercase and the last letter to uppercase, the new string will be "programminG"

Syntax

To get the length of a string, Java's String class provides a length() method.

Below is the syntax for that −

str.length();
Copy after login

Among them, str is a string variable.

To obtain a substring from the original string, the Java String class provides the substring() method.

Syntax

Below is the syntax for that −

str.substring(int startIndex);
str.substring(int startIndex, int endIndex);
Copy after login

Among them, startIndex is included and endIndex is not included.

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)
Copy after login

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

To convert different types of values ​​into string values, Java's String class provides the valueOf() method.

Syntax

Below is the syntax for that −

String.valueOf(Object obj)
Copy after login

Algorithm

Note - This problem can also be solved using the concept of arrays. But here we tried to solve the problem without using array concept. At the same time, we only used the built-in methods of the String class.

Algorithm-1

  • Step 1 - Get the string/word via initialization or 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 - Use the substring() method to get the middle characters (except the first and last characters).

  • Step 4 - Combine the first, middle and last values ​​to get the final string/word.

Algorithm-2

  • Step 1 - Get the string/word via initialization or user input.

  • Step 2 - Get the first and last letter by using charAt() method. Then convert it to lowercase and uppercase using toLowerCase() method and toUpperCase() method respectively.

  • Step 3 - Use charAt() method and for loop to get the middle characters (except the first and last characters).

  • Step 4 - Then combine the first, middle and last values ​​together to get the final string/word.

Multiple Approaches

We have provided the solution in different approaches.

  • By using the built-in 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);
   }
}
Copy after login

Output

Original string is: Java
Updated string is: javA
Copy after login

Approach-2: By Using Inbuilt charAt() Method

Example

In this approach we will utilize 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);
   }
}
Copy after login

Output

Original string: Python
Updated string: pythoN
Copy after login

In this article, we explored how to convert the first letter of a word to lowercase and the last letter of a word to uppercase using different methods.

The above is the detailed content of In Java, capitalize the last letter of a word and lowercase the first letter. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template