You are on page 1of 3

// WAP TO ARRANGE THE LETTERS OF THE WORDS OF A SENTENCE IN ASCENDING ORDER BY SELECTION

SORT

import java.util.*;

class program18

public static void main ( String args[] )

Scanner sc = new Scanner ( System.in );

System.out.println ( " enter a sentence " );

String s = sc.nextLine(); // input a sentence

s = s.trim();

String t = "",t1 = "";

s = s + ' ';

for ( int k=0;k<s.length();k++ )

if ( s.charAt(k) != ' ' )

t = t +s.charAt(k); // word extraction

else

char a[] = t.toCharArray();

for ( int i=0;i<a.length-1;i++ )

int pos = i;
char temp = a[i];

for ( int j=i+1;j<a.length;j++ )

if ( a[j]<temp ) // condition for sorting

temp = a[j];

pos = j;

a[pos] = a[i]; // swaping

a[i] = temp;

t1 = t1 + new String(a)+ " ";

t = "";

System.out.println ( " old sentence " );

System.out.println (s);

s = t1.trim();

System.out.println ( " new sentence " );

System.out.print( s ); // printing new sentence

You might also like