Java Program to Reverse the Words of a String and after Reversal First Word First Letter should be Capital and Last Word First Letter should be Small

In this article you will see how to reverse the words of a string and after reversal first word first letter should be capital and last word first letter should be small by using Java programming language.

Java Program to Reverse the Words of a String and after Reversal First Word First Letter should be Capital and Last Word First Letter should be Small

As per the problem statement you need to reverse a String but the condition is after reversal first word first letter should be capital and last word first letter should be small. So let’s understand it with an example.

Example:

Suppose the string is: I am a boy
Reverse of the original String is: boy a am I
Reverse of the original String based on condition: Boy a am i

Let’s understand it more clearly with a program.

Approach:

  • Declare a String variable and initialize the String value to which you want to reverse.
  • Split the string based on space and stored in an String array.
  • Get the first letter of original String and store it in an character variable say ‘first‘, which will be placed at last in lower case format.
  • Now reverse the original String and store it in an String variable.
  • Get the first letter of reversed String and store it in an character variable say ‘last‘, which will be placed at first in Upper case format.
  • Now print value of ‘last‘ in upper case format by using toUpperCase() method.
  • Convert the reversed string to an array of characters.
  • Then print the array characters except first and last character.
  • Now print value of ‘first‘ in lower case format by using toLowerCase() method.
  • Now you can see the result printed  in output console.

Program:

public class Main
{
    public static void main(String[] args)
    {
        //Declared a String variable 'str' and initialized the value
        String str= new String("I belong to a middle class family"); 
        System.out.println("The original String is: "+str); 
        //Splitted the string based on space and stored in an String array
        String s[] = str.split(" ");
        
        //Got the letter, which will be placed at last in lower case format
        String first=str.substring(0,1);
        
        //declared a String varible 'ans' to hold the reversed String
        String ans = "";
        //Reversing the original String String
        for (int i = s.length - 1; i >= 0; i--)
        {
            ans += s[i] + " ";
        }
        System.out.println("The reversed String is: "+ans); 
        
        //Got the letter, which will be placed at first in Upper case format
        String last=ans.substring(0,1);
        
        System.out.print("The reversed String based on condition is: "); 
        //print the first character in Upper case
        System.out.print(last.toUpperCase()); 
        //converted the reversed string to an array of characters
        char[] ch=ans.toCharArray(); 
        //print the array characters except first and last character
        for(int i=1;i<ch.length-2;i++)
        {  
            System.out.print(ch[i]);  
        }
        //print the last character in lower case
        System.out.println(first.toLowerCase()); 
    }
}

Output:

The original String is: I belong to a middle class family
The reversed String is: family class middle a to belong I 
The reversed String based on condition is: Family class middle a to belong i

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.