Arrange the String in Alphabetical Order
In this program we will input a word from user and arrange it in alphabetical order.
Example : Input : Computer
Output : Cemoprtu
Algorithm and Program given 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
Note : The program is written in the easiest way possible so try understand it. If you face any problem in understanding this program than feel free to contact/comment.
All the Best :)
Keep Learning :)
Comments