An English sentence, separated by spaces, intercepts words and calculates the number of times each word appears in the current English sentence.

Source: Internet
Author: User

Import java. util. hashmap;

/**
* An English sentence is separated by spaces. It intercepts words and calculates the number of times each word appears in the current English sentence.
*
* @ Author ydj
*
*/
Public class interceptwordandcountword {
Private string S;
Private hashmap <integer, string> map;

Public interceptwordandcountword (string s ){
This. S = S + "";
Map = new hashmap <integer, string> ();
}

Private void processmothed (){//This method is too inefficient
System. Out. println ("this English sentence is:" + S + "/T Length:" + S. Length ());
Stringbuffer strbuff = new stringbuffer ();
For (INT I = 0, j = 0; I <S. Length (); I ++ ){
Char c = S. charat (I );
If (C! = "". Charat (0 )){
Strbuff = strbuff. append (C );
} Else {
Map. Put (J, strbuff. tostring ());
Strbuff = new stringbuffer ();
J ++;
Continue;
}
}
For (INT I = 0; I <map. Size (); I ++ ){
Int COUNT = 0;
String S = map. Get (I );
For (Int J = 0; j <map. Size (); j ++ ){
If (S. Equals (Map. Get (j ))){
Count + = 1;
}
}
System. Out. println ("word" + S + "appears in this sentence" + Count + "times ");
}
}

Public static void main (string ARGs []) {
New interceptwordandcountword ("Hello word Hello Friend hello gril welcome to Beijing"). processmothed ();
}
}

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.