icse-promo

Question

Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’ or ‘!’ only. The words are to be separated by a single blank space and are in uppercase.
Perform the following tasks:
a) Check for the validity of the accepted sentence only for the terminating character.
b) Arrange the words in ascending order of their length. If two or more words have the same length, then sort them alphabetically.
c) Display the original sentence along with the converted sentence.

				
					Test your program for the following data and some random data:
Example 1:
INPUT:
AS YOU SOW SO SHALL YOU REAP.
OUTPUT:
AS YOU SOW SO SHALL YOU REAP.
AS SO SOW YOU YOU REAP SHALL
Example 2:
INPUT:
SELF HELP IS THE BEST HELP.
OUTPUT:
SELF HELP IS THE BEST HELP.
IS THE BEST HELP HELP SELF.
Example 3:
INPUT:
BE KIND TO OTHERS.
OUTPUT:
BE KIND TO OTHERS.
BE TO KIND OTHERS
Example 4:
INPUT:
NOTHING IS IMPOSSIBLE#
OUTPUT:
INVALID INPUT
				
			

Share code with your friends

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

Code

				
					import java.util.Scanner;
import java.util.StringTokenizer;
public class Arrange
{
    public static void main(String args[])
    {
        int i=0,j=0,count=0,len=0;
        String sen="";
        String arr[];
        char ch=' ',last=' ';
        /*input sentence,converting it to  upper case,removing unwanted spaces and finding length of sentence Starts here*/
        Scanner sc=new Scanner(System.in);
        System.out.print("Sentence: ");
        sen = sc.nextLine();
        sen = sen.trim();
        sen = sen.toUpperCase();
        len =sen.length();
        /*input sentence,converting it to  upper case,removing unwanted spaces and finding length of sentence ends here*/
        
        /*checking if last character is anything other than ?, ., or !*/
        last =sen.charAt(len - 1);
        if(last!='.'&& last!='?'&& last!='!')
        {
            System.out.println("INVALID INPUT");
            
        }
        else
        {
            System.out.println(sen);
            StringTokenizer st = new StringTokenizer(sen, "? .!,");
            /* counting number of words in sentence*/
            count = st.countTokens();
            String a[] = new String[count];
            /*adding words to array*/
            for(i = 0; i < count; i++)
            {
                a[i] = st.nextToken();
            }
        
            for(i = 0; i < a.length; i++)
            {
                for(j = 0; j < a.length - 1 - i; j++)
                {
                    if(a[j].compareTo(a[j + 1]) > 0)
                    {
                        String temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
                }
            }
        
            for(i = 0; i < a.length; i++)
            {
                for(j = 0; j < a.length - 1 - i; j++)
                {
                    if(a[j].length() > a[j + 1].length())
                    {
                        String temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
                }
            }
            for(i = 0; i < a.length; i++)
            {
                System.out.print(a[i] + " ");
            }
            System.out.println();
        }
    }
}


				
			

Coding Store

Leave a Reply

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