Lexicographical arrangement of letters
in a string_-_
According
to the latest Dictionary paradigms of arrangement of words, this is a simple
program to sort the letters (both upper & lowercase) properly.
The
rules behind sorting are:
1.> The letter having 1st preference is
kept first and the corresponding letters are arranged in decreasing order of
preference.
2.> Any numeric characters are discarded from the
lexicographically arranged string.
3.> Any special character (like !,@, #,$ ,% ,& ,etc)are not considered.
4.> If both the uppercase and
lowercase letters of the same alphabet are present then the uppercase letter is
given the greater preference.
Examples:--
Input:
ADCBadcb
Output
AaBbCcDd
Coding:___________________________________
import
java.util.Scanner;
//Program
to arrange a string lexicographically by Agniva mukhopadhyay
public
class Lexico
{
public String bblSort(String s)//bubblesort
of characters
{
int n = s.length();String str =
"";
char array[] = new char[n];
for (int c = 0; c < n; c++)
array[c] = s.charAt(c);
for (int c = 0; c < ( n - 1 ); c++)
{
for (int d = 0; d < n - c - 1;
d++) {
if (array[d] > array[d+1]) /* For
ascending order use < */
{
char swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
for (int c = 0; c < n; c++)
str += array[c] ;
return str; //returning sorted string
}
public void lex(String s){
String st = bblSort(s);
String up,lo,fin = "";
int x = st.length(),i,u,l;
for(i = 0;i<x;i++){
if(
Character.isLowerCase(st.charAt(i))){break;}
}
up = st.substring(0,i);//Uppercase
String
lo =
st.substring(i,x);//Lowercase String
u = up.length();//length of up
l = lo.length();//length of lo
for(int k = 0;k<26;k++){//loop
iterates from a-z
for(int j = 0;j<u;j++){//loop for
uppercase string
int c =
(int)((up.charAt(j)-'A'));//converting character to values ranging from 0-25
if( c == k){fin+= up.charAt(j);
}
}
for(int j = 0;j<l;j++){//loop
for lowercase string
int c =
(int)((lo.charAt(j)-'a'));//same as for previous loop
if( c == k){fin+= lo.charAt(j);
}
}
}
System.out.println(fin);//returning
lexicographically arranged string
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
Lexico ob = new Lexico();
String f = "";
do{
System.out.println("/////////////////Arrange
Lexicographically");
System.out.print("ENTER A STRING :
");
String s = sc.next();
System.out.print("THE
LEXICOGRAPHICALLY Arranged String:");
ob.lex(s);
System.out.println("-----------------------------------------");
System.out.println(" WANNA
CONTINUE WITH SOME OTHER STRING??(y/n)");
f = sc.next();
}while(f.equalsIgnoreCase("y"));
}
}
SNAPSHOTS…………>>>>>>>
0 comments :
Post a Comment