Write a program that sets up a String variable containing a paragraph of text of your choice. Extract the words from the text and sort them into alphabetical order. Display the sorted list of the words. Also display no of white space, no of Capital characters and number of vowels in the text. Replace the white space with “/” and display the string in the reverse order.
Code for Program sets up a String variable containing a paragraph of text of your choice. Extract the words from the text and sort them into alphabetical order in Java
import java.util.StringTokenizer;
classstring
{
publicstaticvoid main(String args[])
{
operations op = new operations();
op.getToken();
op.sort();
op.diffOp();
}
}
class operations
{
static String in="My first name is Vidyadhar and my last name is Yagnik";
static String arr[]=new String[30];
staticint c=0;
void getToken()
{
StringTokenizer st=new StringTokenizer(in," ");
while(st.hasMoreTokens())
{
arr[c]=st.nextToken();
c++;
}
System.out.println("The original string is:::"+in);
System.out.println("-------The Tokens of the strings--------");
for(int i=0;i<c;i++)
{
System.out.println(arr[i]);
}
}
void sort()
{
for(int i=0;i<c;i++)
{
for(int j=i+1;j<c;j++)
{
if(arr[j].compareToIgnoreCase(arr[i]) < 0)
{
String t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
System.out.println("--------The sorted array of strings---------");
for(int k=0;k<c;k++)
{
System.out.println(arr[k]);
}
}
void diffOp()
{
int space=0,capital=0,vowel=0;
//Count no. of white spaces,capital letters,vowels from the stringchar temp;
for(int i=0;i<in.length();i++)
{
temp=in.charAt(i);
if(temp==' ')
space++;
if(temp>=65 && temp<=90)
capital++;
if(temp=='a' || temp=='A' || temp=='e' || temp=='E' || temp=='i' || temp=='I' || temp=='o' || temp=='O' || temp=='u' || temp=='U')
vowel++;
}
System.out.println("The no. of white space is::"+space);
System.out.println("The no. of capital letter is::"+capital);
System.out.println("The no. of vowel is::"+vowel);
//replace space with '/'
String newString=new String(in);
newString=newString.replace(' ','/');
System.out.println("The replaced string is:::"+newString);
//reverse string
StringBuffer revers=new StringBuffer(in);
revers.reverse();
System.out.println("The reverse string is:::"+revers);
}
}
/*
Output
java string
The original string is:::My first name is Vidyadhar and my last name is Yagnik
-------The Tokens of the strings--------
My
first
name
is
Vidyadhar
and
my
last
name
is
Yagnik
--------The sorted array of strings---------
and
first
is
is
last
my
My
name
name
Vidyadhar
Yagnik
The no. of white space is::10
The no. of capital letter is::3
The no. of vowel is::13
The replaced string is:::My/first/name/is/Vidyadhar/and/my/last/name/is/Yagnik
The reverse string is:::kingaY si eman tsal ym dna rahdaydiV si eman tsrif yM
*/