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 ofInstance-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 ofInstance-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();
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);
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)
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)
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); } }
Output
Original string is: Java Updated string is: javA
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); } }
Output
Original string: Python Updated string: pythoN
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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

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

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.

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.
