Table of Contents
Show you some examples
Instance-3
Syntax
Algorithm-2
Multiple Approaches
Approach-1: By Using Inbuilt substring Method
Example
Output
Approach-2: By Using Inbuilt charAt() Method
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

Aug 20, 2023 am 10:05 AM
java word capital

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles