Monday, 11 February 2019

WordsInSentence2012_2




import java.util.*;
class WordsInSentence2012_2
{
Scanner sc = new Scanner(System.in);
    public void take()
    {
     
        String str, words[], stk="";
        int i,j,c=0,flag, len;
        char ch;
        while(true)
        {
            flag=0;
            System.out.println("Enter the sentence:");
            str=sc.nextLine();
            len= str.length();
            words=new String[len];
            for(i=0;i<len; i++)
            {
                if(Character.isLowerCase(str.charAt(i)))
                {
                    flag=1;
                    break;
                }
            }
            if (flag==0)
                break;
            else
                System.out.println("Enter the sentence again with all uppercase letters");
        }
        i=0;
        while(i<len)
        {
            ch=str.charAt(i);
            if(ch==' '|| ch=='.' || ch=='?' || ch=='!')
            {
                words[c]=stk;
                c++;
                i++;
                stk="";
            }
            else
            {
                stk+=ch;
                i++;
            }
        }
        for(i=0;i<c;i++)
        {
            for(j=0;j<c-1;j++)
            {
                if((words[j].compareTo(words[j+1]))>0)
                {
                    stk=words[j];
                    words[j]=words[j+1];
                    words[j+1]=stk;
                }
            }
        }
        System.out.println("Length= "+c);
        System.out.println("\nRearranged Sentence:\n");
        for(i=0;i<c;i++)
            System.out.print(words[i]+" ");
    }
    public static void main(String args[])
    {
        WordsInSentence2012_2 ob=new WordsInSentence2012_2();
        ob.take();
    }
}

No comments:

Post a Comment