Arranging given Word in Alphabetical Order in JAVA

Arrange the String in Alphabetical Order


Example : Input : Computer
                  Output : Cemoprtu

Algorithm and Program Below :-


Algorithm :-
  • Input a word
  • Find its length
  • Start a for loop in alphabetical order of Ascii Code
  • Inside the Ascii code loop, start another loop and extract character from the string
  • if the character is same as the given ascii code
  • Store it in another variable
  • close both the loops
  • Print this variable, This will display string in Alphabetical order.

Program :-

import java.io.*;
import java.util.*;
class Alphabetical
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        String str,str1="";
        char ch,ch1; int l,i,j;
        System.out.print("Enter Word : ");
        str=in.nextLine();
        l= str.length();
        for(i=65;i<=90;i++) // Ascii code of Alphabetical Order
        {
            for(j=0;j<l;j++)
            {
                ch=str.charAt(j); // Extracting word from the string
                if(ch==(char)i||ch==(char)i+32) // if extracted word is same as the given Ascii code
                {
                    str1=str1+ch; // Storing string in alphabetical order
                }
            }
        }
        System.out.println("Original Word : "+str);
        System.out.println("New Word : "+str1);
    } // end of main
} // end of class


All the best :)

Comments

Popular posts from this blog

Frequency of each digit of a number in Java

Trimorphic Number in JAVA

Tri-Automorphic Number in JAVA