Wednesday, May 15, 2013

BlueJ program to enter characters and arrange them according to their ASCII values



In this java program, the user will enter character values and the equivalent ASCII values will be stored in another numeric array. The characters will be arranged in ascending order and display the values.

import java.io.*;
class A
{
 char ch[],c;
 int arr[];
 BufferedReader br;
 int x=0,i,j,t;
 A()
 {
  ch= new char[100];
  arr=new int[100];
  }
  public void take() throws Exception
  {
   while(true)
   {
     br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter a character:");
     c=(char)br.read();
     arr[x]=(int)c;
     ch[x++]=c;
     if (x==10)
     break;
     }
    }
     public void show()
     {
      System.out.print("\n\nEntered characters=");
      for(i=0;i< x;i++)
      {
       System.out.print(" "+ch[i]);
       }
       for(i=0;i< x-1;i++)
       {
        for(j=i+1;j < x;j++)
       {
        if(arr[i] >arr[j])
        {
         t=arr[i];
         arr[i]=arr[j];
         arr[j]=t;
         c=ch[i];
         ch[i]=ch[j];
         ch[j]=c;
         }
         }
         }
      System.out.println("\n\nSorted characters with their ASCII values=\n");
      for(i=0;i< x;i++)
      {
       System.out.println(arr[i]+ " "+ch[i]);
       }
      }
      public static void main(String args[]) throws Exception
      {
       A ob=new A();
       ob.take();
       ob.show();
       }
       }

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner