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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.