查找单词的题目,很有意思,我提供了两个版本

//Exercise
//9.39:
//已知有如下 string 对象:string line1 = "We were her pride of 10 she named us:"; 
                                            string line2 = "Benjamin, Phoenix, the Prodigal"
                                            string line3 = "and perspicacious pacific Suzanne";
                                            string sentence = line1 + ' ' + line2 + ' ' + line3;
//编写程序计算 sentence 中有多少个单词,并指出其中最长和最短的单词。如果有多个最长或最短的单词,则将它们全部输出。
//版本一:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;


int main()
{
   string line1="we are her pride of 10 she named us:";
   string line2="benjamin, phoenix, the  prodigal";
   string line3="and perspicacious pacific suzanen";
   string sentence=line1+" "+line2+" "+line3;
   vector<string> longest_word,smallest_word;
   unsigned int count=0;
   string::size_type s_size_big=0,s_size_small=0;
   string word;
   istringstream in_stream(sentence);
   cout<<"The sentence is :"<<endl;
   while(in_stream>>word)
   {
      count++;
      cout<<word<<" ";
      string::size_type size_tmp=word.size();
      if(count==1)
      {
        s_size_big=s_size_small=size_tmp;
        longest_word.push_back(word);
        smallest_word.push_back(word);
      }
      else
      {
         if(size_tmp>s_size_big)
         {
             s_size_big=size_tmp;
             longest_word.clear();
             longest_word.push_back(word);
         }
         else
         {
            if(size_tmp==s_size_big)
            {
                longest_word.push_back(word);
            }
            else
            {
               if(size_tmp<s_size_small)
               {
                  s_size_small=size_tmp;
                  smallest_word.clear();
                  smallest_word.push_back(word);
               }
               else
               {
                    if(size_tmp==s_size_small)
                    smallest_word.push_back(word);
               }
            }
          }
        }
     }


    cout<<endl;
    cout<<"The total number of the words : "<<count<<endl;
    cout<<"The longest word is:"<<endl;
    for(vector<string>::iterator big_iter=longest_word.begin();big_iter!=longest_word.end();
        big_iter++)
    cout<<*big_iter<<" ";
    cout<<endl;
    cout<<"The smallest word is:"<<endl;
    for(vector<string>::iterator small_iter=smallest_word.begin();small_iter!=smallest_word.end();
        small_iter++)
    cout<<*small_iter<<" ";
    cout<<endl;
 }

//下面是运用find_first_of和find_first_not_of的版本:
/*在答案书的答案上,那个答案是错的!*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;


int main()
{
   string line1="we are her pride of 10 she named us:";
   string line2="benjamin, phoenix, the  prodigal";
   string line3="and perspicacious pacific suzanen";
   string spaces(" \t:,\n\v\r\f");//后面都一些转义字符
   string sentence=line1+" "+line2+" "+line3;
   vector<string> longest_word,smallest_word;
   unsigned int count=0;
   string word;
   string::size_type s_size_big=0,s_size_small=0;
   string::size_type start_pos=0, end_pos=0;
   while((start_pos=sentence.find_first_not_of(spaces,end_pos))!=string::npos)
   {
       ++count;
       string::size_type size_tmp;
       end_pos=sentence.find_first_of(spaces,start_pos); //这个是代码的核心部分
       if(end_pos==string::npos)//如果没有匹配的话就返回npos,这是一个非常大的数,比任何的合法的下标都大
       size_tmp=sentence.size()-start_pos;
       else
       size_tmp=end_pos-start_pos;
       word.assign(sentence,start_pos,size_tmp);
       cout<<"the size of sentence: "<<sentence.size()<<endl;
       cout<<word<<" "<<"length:"<<size_tmp<<endl;
       if(count==1)
      {
        s_size_big=s_size_small=size_tmp;
        longest_word.push_back(word);
        smallest_word.push_back(word);
      }
      else
      {
         if(size_tmp>s_size_big)
        


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值