• C++ primer 练习9.49


       如果一个字母延伸到中线之上,如d或f,则称其有上出头部分。如果一个字母延伸到中线之下,如p或g,

    则称其有下出头部分。编写程序,读入一个单词,输出最长的即不包含上出头部分,也不包含下出头部分单

    词。

    // 9_49.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include<string>
    #include<iostream>
    using namespace std;
    
    string& func(string &str)
    {
        string ascenderAndDescender = "bdfghjklpqty";//把上出头部分和下出头部分的字母包含进来
        static string maxLengthStr = "";             //用来储存最长的要求字符串
        int pos = 0,posAAD=0;                        //pos用来表示所要求字符的位置,posADD用来表示上出头或者下出头字符的位置
        while ((pos = str.find_first_not_of(ascenderAndDescender, posAAD)) != str.npos)//求第一个所要求字符的位置
        {
            if ((posAAD = str.find_first_of(ascenderAndDescender, pos)) != str.npos&&maxLengthStr.size() < (posAAD - pos))
            {                                                            //求第一个上出头或者下出头字符的位置
                maxLengthStr = string(str,pos,posAAD-pos);               //如果新的所要求的字符串长度教大,赋予它新值
            }
        }
        if (pos = str.find_last_not_of(ascenderAndDescender,0) && maxLengthStr.size() < (str.length() - pos))
            maxLengthStr = string(str,pos);                              //这是为了检验有可能在最后的情况
        return maxLengthStr;
    }
    
    int main()
    {
        string str = "asaaaaaaashgasjgpdhgasjqwghnaanbmnna";
        cout << func(str) << endl;
        return 0;
    }

    // 9_49.cpp : 定义控制台应用程序的入口点。//
    #include "stdafx.h"#include<string>#include<iostream>using namespace std;
    string& func(string &str){string ascenderAndDescender = "bdfghjklpqty";//把上出头部分和下出头部分的字母包含进来static string maxLengthStr = "";             //用来储存最长的要求字符串int pos = 0,posAAD=0;                        //pos用来表示所要求字符的位置,posADD用来表示上出头或者下出头字符的位置while ((pos = str.find_first_not_of(ascenderAndDescender, posAAD)) != str.npos)//求第一个所要求字符的位置{if ((posAAD = str.find_first_of(ascenderAndDescender, pos)) != str.npos&&maxLengthStr.size() < (posAAD - pos)){                                                            //求第一个上出头或者下出头字符的位置maxLengthStr = string(str,pos,posAAD-pos);               //如果新的所要求的字符串长度教大,赋予它新值}}if (pos = str.find_last_not_of(ascenderAndDescender,0) && maxLengthStr.size() < (str.length() - pos))maxLengthStr = string(str,pos);                              //这是为了检验有可能在最后的情况return maxLengthStr;}
    int main(){string str = "asaaaaaaashgasjgpdhgasjqwghnaanbmnna";cout << func(str) << endl;    return 0;}

  • 相关阅读:
    android api 中文 (74)—— AdapterView.AdapterContextMenuInfo
    Android API 中文(76)——AdapterView.OnItemLongClickListener
    Android 中文api (81)——InputMethod [输入法]
    android 中文 api (87) —— BaseInputConnection
    Android 中文API合集(4)(102篇)(chm格式)
    Android中文API(97)—— ContextMenu
    android api 中文 (75)—— AdapterView.OnItemClickListener
    android 中文api (84) —— TrafficStats
    Windows XP 上安装 Bind9
    数据库索引实例之三
  • 原文地址:https://www.cnblogs.com/csudanli/p/5338272.html
Copyright © 2020-2023  润新知