0% found this document useful (0 votes)
30 views6 pages

Algorithm Stringsort

The document describes a program that accepts a sentence as input, sorts the words in the sentence by length or alphabetically if same length, and outputs the original and sorted sentences. It checks that the input sentence is terminated by a period, question mark, or exclamation point.

Uploaded by

Clary June
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views6 pages

Algorithm Stringsort

The document describes a program that accepts a sentence as input, sorts the words in the sentence by length or alphabetically if same length, and outputs the original and sorted sentences. It checks that the input sentence is terminated by a period, question mark, or exclamation point.

Uploaded by

Clary June
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Question 13

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 UPPER CASE.

Perform the following tasks:

 Check for the validity of the accepted sentence only for the
terminating character.
 Arrange the words in ascending order of their length. If two or
more words have the same length, then sort them alphabetically.
 Display the original sentence along with the converted sentence.

Coding:

import java.util.*;
import java.io.*;
public class StringSort
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence that terminates with '.' '?' or
'!' ");
String str=sc.nextLine().toUpperCase();
int l = str.length();

int c=str.charAt(l-1);
if(c=='.' || c=='?' || c=='!')
{
str=str.substring(0,str.length()-1);
StringTokenizer st = new StringTokenizer(str);
int ct= st.countTokens();
String s[]= new String[ct];
for(int i=0;i<ct;i++)
{
s[i]=st.nextToken();
}
for(int i=0;i<ct-1;i++)
{
for(int j=0;j<ct-i-1;j++)
{
if(s[j].length() > s[j+1].length())
{
String temp = s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
else if(s[j].length()==s[j+1].length() &&
s[j].compareTo(s[j+1])>0)
{
String temp = s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
System.out.println(str);
for(int i=0;i<ct;i++)
{
System.out.print(s[i]+" ");
}
}
else
{
System.out.println("invalid input ");
}
}
}
ALGORITHM
1. [ To read a sentence from the user]
Read str
2. [ to convert str to upper case]
str = uppercase of str
3. [to find the length of the str]
l=length of str
4.[to find the terminating character]
c= last character of str
5.[ to check whether the terminating character is ‘ .’ or ‘?’ or ‘!’]
If( c=’.’ || c=’?’ || c=’!’) goto step 6
else goto step 18
6. [to remove the terminating character]
str= string without last charcter
7. [ to break the sentence into separate words &
to count how many words are there in the sentence]
ct= number of words in str
8.[ array to store the words]
s[]= array to store the words
9.[loop and loop variable]
i=0
To repeat step 11 until i<ct
10. s[i]= storing each words in the sentence
i = i+1
11. [outer loop]
Repeat step 12 and 15 until i<ct-1
12.[inner loop and loop variable]
j=0
Repeat step 13 and 14 until j<ct-i-1
13. [to sort the sentence]
If( length of s[j] > s[j+1])
temp = s[j]
s[j] = s[j+1]
s[j+1] = temp
else if (length of s[j] = length of s[j+1] and if s[j] compared to
s[j+1] lexicographically >0)
temp = s[j]
s[j] = s[j+1]
s[j+1] = temp
14. j = j+1
15. i = i+1
16. [to print the original sentence]
Display (str)
17.[loop]
Repeat step 18 until i<ct
18.[ to print the sorted sentence]
Display( s[i] + “ “)
i= i+1
19. [to show that it is an invalid input]
Display ( “ invalid input”)
20. Stop

You might also like