Friday 24 October 2014

Find the longest and the smallest word in a sentence and print their length.

//Find the longest and the smallest word in a sentence and print their length.

import java.io.*;
class LongestSmallest1
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string");
String s=br.readLine();
s=s+" ";
int l=s.length();
String word="", lword="", sword="";
int max=0,min=0;

for(int i=0;i<l;i++)
{
char x=s.charAt(i);
if(x!=' ')
word=word+x;
else
{
if(word.length()>max)
{
max=word.length();
lword=word;
    }
 word="";
    }
    }
     
for(int i=0;i<l;i++)
{
char x=s.charAt(i);
if(x!=' ')
min=min+x;
else
break;
    }
    
for(int i=0;i<l;i++)
{    
char x=s.charAt(i);
if(x!=' ')
word=word+x;
else
{
if(word.length()< min)
{
min=word.length();
sword=word;
    }
 word="";
    }
    }
    System.out.println("Longest word : " + lword);
    System.out.println("Length of Longest word : " + max);
    System.out.println("Smallest word : " + sword);
    System.out.println("Length of Smallest word : " + min);
    }
    }

1 comment:

  1. mercury casino login【VIP】best 【VIP】best
    메리트 카지노 Mercury 바카라사이트 Casino login】,【WG98.vip】⚡,cric online casino,canada casino bonus,cric live casino,online 메리트카지노 roulette.

    ReplyDelete