Array-promo

Question

Sort words in descending order of their length and words with the same length should be sorted alphabetically.

				
					Enter the Sentence:
To handle yourself use your head and to handle other use your heart.
Yourself Handle Handle Heart Other Head Your Your And Use Use To To.
Enter the Sentence:
This is human resource department.
Department Resource Human This Is.
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;

public class SortWord
{
    public static void main()
    {
        int i,len=0,j=0,noOfWords=0;
        char ch= ' ';
        String sen="",wd="",temp="";
        String arr[];
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the Sentence:");
        sen=sc.nextLine();
        len=sen.length();
        if(sen.charAt(len-1)!='.')
        {
            sen=sen+".";
            len=len+1;
        }
        for(i=0;i< len;i++)
        {
            ch=sen.charAt(i);
            if(ch==' '||ch=='.')
            {
                noOfWords++;
            }
        }
        arr=new String[noOfWords];
        for(i=0;i< len;i++)
        {
            ch=sen.charAt(i);
            if(ch==' '||ch=='.')
            {

                arr[j]=wd;
                wd="";
                j++;
            }
            else
            {     
                if(i==0||sen.charAt(i-1)==' ')
                {
                    ch=Character.toUpperCase(ch);
                }
                wd=wd+ch;
            }
        }
        for(i=0;i< noOfWords;i++)
        {
            for(j=0;j< noOfWords;j++)
            {

                if(arr[i].length()>arr[j].length())
                {
                    temp=arr[i];
                    arr[i]=arr[j];
                    arr[j]=temp;
                }

                else if(arr[i].length()==arr[j].length())
                {
                    //CompareTo compare two strings
                    //Here if arr[i] is before arr[j] in dictionary then it returns value less than 0
                    //else it will return value greater than 1 
                    // example arr[i]=cat and arr[j]=bat,we can see cat is put after bat in dictionary therefore 
                    //it will return value greater than 0
                    if(arr[i].compareTo(arr[j])< 0)
                    {
                        temp=arr[i];
                        arr[i]=arr[j];
                        arr[j]=temp;
                    }
                }

            }
        }

        for(i=0;i< noOfWords;i++)
        {
            if(i!=noOfWords-1)
            {
                System.out.print(arr[i]+" ");
            }
            else
            {
                System.out.print(arr[i]);   
            }

        }
        System.out.print(".");
    }
}


				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *