Computer Science, asked by hremnt, 1 year ago

Wap in java to enter a string and display the longest word and the length of the longest word present in the string

Answers

Answered by kvnmurty
10

// the syntax needs to be correctly written.  this is pseudo code...
//  tokensplit.java   file

package  .....;
import java.util.Scanner;
import java.io.IOException;
import java.util.lang;

public   class  demo {

    public static void main (String args[])  {

        String  longest, inpStr, str ;
        int cnt = 0, wordNumber = 0, lineNumber = 0;

        // read the input string..
       Scanner     inpStr  = new  Scanner (System.in);
       String delims = "[ .,?\\n\\t()]+" ;     // specify the word delimiter characters
       length = 0; 
       str = inpStr.nextLine();

       while (! str.isEmpty())  {
             lineNum++;
            String [] tokens = str.split (delims);
       
            for (String t  :  tokens)  {   // iterate over all tokens in str
                 wordNumber ++ ;
                 if (t.length() > length) {
                            length = t.length();   longest = t;
                  }
            } // for
           str = inpStr.nextLine();

      }  // while

      System.out.println("longest word: " + longest + "\t its length:  " + length + "in line: ", + lineNum + "  word number: " + wordNum);

} // class demo




kvnmurty: click on thanks button above please
Similar questions