Q.Write a java program to count the number of words in the entered sentence
import java.io.*;
public class CountWords
{
public static void main(String args[])throws Exception
{
BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the sentence: ");
String s=stdin.readLine();
s=' '+s;
char ch;
int w=0;
for(int i=0;i<s.length();i++) //loop is used to extract each character in the sentence
{
ch=s.charAt(i);
if(ch==' ')
{
w++;
}
}
System.out.println("Number of word in the sentence:"+w);
} //method
} //class
Q.Write a program to accept a word and arrange the letters of words in alphabetical order
Sample input: computer
output:cemoprtu
import java.io.*;
public class WordAlbha
{
public static void main(String args[])throws Exception
{
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader stdin=new BufferedReader(isr);
System.out.print("Enter the word:");
String word=stdin.readLine();
char ch;
for(int i=65;i<=90;i++)
{
for(int j=0;j<word.length();j++)
{
ch=word.charAt(j);
ch=Character.toLowerCase(ch);
if(ch==(char)i||ch==(char)(i+32))
System.out.print(ch);
}
}
}
}
public class WordAlbha
{
public static void main(String args[])throws Exception
{
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader stdin=new BufferedReader(isr);
System.out.print("Enter the word:");
String word=stdin.readLine();
char ch;
for(int i=65;i<=90;i++)
{
for(int j=0;j<word.length();j++)
{
ch=word.charAt(j);
ch=Character.toLowerCase(ch);
if(ch==(char)i||ch==(char)(i+32))
System.out.print(ch);
}
}
}
}