C ++ primer exercise 9.39

Source: Internet
Author: User

# Include <iostream>
# Include <string>
# Include <vector>
Using namespace: STD;

Int main ()
{
String S1 ("* We were her pride of 10 she named us :*;");
String S2 ("* Benjamin, Phoenix, the prodigal *");
String S3 ("and perspicacious Pacific Suzanne *;");
String S = S1 + "" + S2 + "" + S3;
String F (",*:;");
String: size_type BPOs = 0;
String: size_type EPOS = 0;
Int num = 0;
Int Len = 0;
Int maxlen = 0;
Int minlen = 0;
Vector <string> maxstring, minstring;

Cout <"The sentense need to process is" <Endl;
Cout <S <Endl;

While (BPOs = S. find_first_not_of (F, BPOs ))! = String: NPOs)
{
++ Num;
EPOS = S. find_first_of (F, BPOs );

If (EPOs = string: NPOs)
Len = S. Size ()-BPOs;
Else
Len = EPOS-BPOs;
Cout <"processing the word" <S. substr (BPOs, Len) <Endl;
If (LEN> maxlen)
{
Maxstring. Clear ();
Maxstring. push_back (S. substr (BPOs, Len ));
Maxlen = Len;
}
Else if (LEN = maxlen)
Maxstring. push_back (S. substr (BPOs, Len ));
If (num = 1)
{
Minlen = Len;
Minstring. push_back (S. substr (BPOs, minlen ));
}
Else {
If (LEN <minlen)
{
Minlen = Len;
Minstring. Clear ();
Minstring. push_back (S. substr (BPOs, minlen ));
}
Else if (LEN = minlen)
Minstring. push_back (S. substr (BPOs, Len ));
}
BPOs = EPOS;
}

Cout <"there is" <num <"Words in the sentense." <Endl;
Cout <"the longest word:" <Endl;
For (vector <string >:: size_type IX = 0; ix! = Maxstring. Size (); ix ++)
Cout <maxstring [ix] <Endl;
Cout <"The smllest word:" <Endl;
For (vector <string >:: size_type IX = 0; ix! = Minstring. Size (); ix ++)
Cout <minstring [ix] <Endl;

System ("pause ");
Return 0;
}

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.