import java.io.*; class AlphabeticalOrder { static String n; static int l; public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a word : "); n = br.readLine(); l = n.length(); alphabetical(); } public static void alphabetical() { char b[] = new char[l]; for(int i=0;i<l;i++) b[i] = n.charAt(i); char t; for(int j=0;j<l-1;j++) { for(int k=0;k<l-1-j;k++) { if(b[k]>b[k+1]) { t=b[k]; b[k]=b[k+1]; b[k+1]=t; } } } System.out.println("\nOriginal word : " +n); System.out.print("Sorted word : "); for(int m=0;m<l;m++) System.out.print(b[m]); System.out.print("\n"); } }
ALGORITHM:-
1. Start
2. Accept a word from the user.
3. Convert each letter of the word to its corresponding ASCII value.
4. Compare each letter of the word by its next letter and swap them if the first letter has a greater ASCII value than the next one.
5. Repeat the above procedure until the entire word is sorted.
6. Print the new word.
7. End
OUTPUT:-
Enter a word : computer
Original word : computer
Sorted word : cemoprtu
Related Programs:-
★ Display the solution of Towers of Hanoi using recursion
★ Convert a number into words
★ Reverse a word or sentence ending with '.' using recursion
★ Convert Decimal to Octal
★ Convert Decimal to Binary
Thanks
ReplyDeleteShare java code for print string without specific letter.
ReplyDeleteEx: Input value-- selva@gmail.com
Output value-- selvagmailcom (Remove @&.)
////can i write like this......? ///
ReplyDeleteclass Abcd
{
public static void main(String args[])
{
int i=0,j=0;
char a1[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char b1[]={'r','a','k','e','s','h'};
for(i=0;i<=25;i++)
for(j=0;j<=5;j++)
{
if(a1[i]==b1[j])
System.out.println(a1[i]);
}
}
}
good bro...
Deletevery useful
ReplyDeletecan anyone please give me the code for:
ReplyDeleteIf i am entering any word which is not in arranged manner then after clicking enter it should give the rearrange this with meaningfull word at console page.
Word string length may consist 0-infinite length of letters
Faltu hai
ReplyDeleteThanks
ReplyDeletearrange the string given order.
ReplyDeleteinput : ( pro@gr98am)
output : (program@98)
Write A Program for this:
ReplyDeleteInput-This is a zoo
Output-sihT si a ooz
public class Alphabets
ReplyDelete{
public void disp(String str)
{
int i,j,p;
char chr;
p=str.length();
for(i=65;i<=90;i++)
{
for(j=0;j<p;j++)
{
chr=str.charAt(j);
if(chr==(char)i||chr==(char)(i+32))
System.out.println(chr);
}
}
}
}
//a better program
Thanks
ReplyDeleteAnyone please give me the output for this string ("I know java")
ReplyDelete