Monday, December 16, 2024

BlueJ Program To Display Words of A Sentence In Alphabetical Order

 import java.util.*;
class Sen
{   
 String arr[]=new String[15];
    String str,t;
    int i,j,c;
    Scanner sc=new Scanner(System.in);
    public void show() 
    {
       System.out.print("\nEnter the sentence:");
      str=sc.nextLine()+" ";
      c=0;
      while(true)
      {
          i=str.indexOf(' ');
          if(i<0)
          break;
          arr[c++]=str.substring(0,i);
          str=str.substring(i+1);
      }
       
     for(i=0;i<c-1;i++)
     {
         for(j=i+1;j<c;j++)
     {
              if(arr[i].compareToIgnoreCase(arr[j])>0)
              {
                  t=arr[i];
                  arr[i]=arr[j];
                  arr[j]=t;
              }
     }
    }
     
     System.out.print("\nWords in alphabetical order\n");
     for(i=0;i<c;i++)
     {
         
        System.out.print(" "+arr[i]);
    }
    
       } 
    }
         

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner