Java to find the longest and most frequently used words in a string

Source: Internet
Author: User
Import java. Io .*;
Import java. util .*;

Public class maxlength {// find the word with the longest Length

Static void max (int A [], string mystr []) {
String [] sigstr;
Int max = A [0];
For (INT I = 0; I <A. length; I ++ ){
If (A [I]> MAX) max = A [I];

}
// System. Out. println (max );
For (INT I = 0; I <A. length; I ++) {// find the subscript and output of the longest word
If (A [I] = max) system. Out. println ("the longest word is:" + mystr [I]);

}

}

Static void Nums (string mystr []) {// find the most frequently occurring word and Output
Int [] A = new int [mystr. Length];
For (INT I = 1; I <mystr. length; I ++ ){
A [I] = 1;
}


For (INT I = 1; I <mystr. length; I ++)
For (Int J = I-1; j> = 0; j --){
If (mystr [I]. Equals (mystr [J]) A [I] ++;
}

Int max = A [0];
For (INT I = 0; I <A. length; I ++ ){
If (A [I]> MAX) max = A [I];

}
// System. Out. println (max );

For (INT I = 0; I <A. length; I ++ ){
If (A [I] = max) system. Out. println ("the most frequently used word is:" + mystr [I]);

}
}

Public static void main (string [] ARGs) throws exception {
Bufferedreader BR = new bufferedreader (New inputstreamreader (system. In ));
String STRs = Br. Readline ();
String [] mystr = STRs. Split (""); // enter a string of words, separated by spaces, where each word is stored in an array
Int [] A = new int [mystr. Length];
For (INT I = 0; I <mystr. length; I ++ ){
A [I] = 0;

}

For (INT I = 0; I <mystr. length; I ++ ){

A [I] = mystr [I]. Length ();
}

Max (A, mystr); // find the word with the longest Length
Nums (mystr); // find the word that appears most frequently and Output
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.