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 = ,posAAD=; //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,) && maxLengthStr.size() < (str.length() - pos))
maxLengthStr = string(str,pos); //这是为了检验有可能在最后的情况
return maxLengthStr;
} int main()
{
string str = "asaaaaaaashgasjgpdhgasjqwghnaanbmnna";
cout << func(str) << endl;
return ;
}
// 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;}
C++ primer 练习9.49的更多相关文章
- 【足迹C++primer】49、超载,变化,运营商
超载,变化,运营商 Conversion Operators 转换操作符 operator type() const Conversions to an array or a function typ ...
- C++primer 9.49
题目:如果一个字母延伸到中线之上,如d或f,则称其有上出头部分(ascender).如果一个字母延伸到中线之下,如p或g,则称其有下出头部分(descender).编写程序,读入一个单词文件,输出最长 ...
- 【足迹C++primer】49、超载,更改,运营商
超载,更改.运营商 Conversion Operators 转换操作符 operator type() const Conversions to an array or a function typ ...
- 转载:看c++ primer 学习心得
学习C++ Primer时遇到的问题及解释 chenm91 感觉: l 啰嗦有时会掩盖主题:这本书确实有些啰嗦,比如在讲函数重载的时候,讲了太长一大段(有两节是打了*号的,看还是不看 ...
- c++ primer plus 第6版 部分二 5- 8章
---恢复内容开始--- c++ primer plus 第6版 部分二 5- 章 第五章 计算机除了存储外 还可以对数据进行分析.合并.重组.抽取.修改.推断.合成.以及其他操作 1.for ...
- c++ primer plus 第6版 部分一 1-4章
c++ primer plus 第6版 源代码 ---编译器---目标代码---连接程序(启动代码--库代码)---可执行代码 源代码扩展名:c cc cxx C cpp ...
- 《C++ Primer Plus》学习笔记2
<C++ Primer Plus>学习笔记2 第五章 循环和关系表达式 ========================================================== ...
- C Primer Plus 学习体会
本月刚刚开始学习<C primer plus>,之前课上草草学过一遍,讲到指针就结束了.现在重新开始看感觉难度不大只是刚开始接触有些语言细节比较琐碎.学习这一周的体会如下: 诸多前辈推荐的 ...
- 49. 3种方法实现复杂链表的复制[clone of complex linked list]
[本文链接] http://www.cnblogs.com/hellogiser/p/clone-of-complex-linked-list.html [题目] 有一个复杂链表,其结点除了有一个ne ...
随机推荐
- TKinter之文本域与多窗口
用tkinter做出一个文本框出来,用于写字 代码很简单: #!/usr/bin/env python # _*_ coding:utf-8 _*_ from Tkinter import * roo ...
- apache commons vfs 文件夹监控
package test.vfs; import java.io.File; import org.apache.commons.logging.Log; import org.apache.comm ...
- 简单了解ddos攻击
1.一种为流量攻击,主要是针对网络带宽的攻击,即大量攻击包导致网络带宽被阻塞,合法网络包被虚假的攻击包淹没而无法到达主机: 2.另一种为资源耗尽攻击,主要是针对服务器主机的攻击,即通过大量攻击包导致主 ...
- openstack(liberty): 简单网络连接图
openstack起初的网络部分是和计算核心nova合在一起的,后来被拆分出来,独立成为一个模块, 现在名为Neutron. 本博文是学习记录,记录的是基于GRE tunnel技术的neutron和计 ...
- 【shell】while与until循环
while循环 #!/bin/bash i=1 s=0 while [ $i -le 100 ] do s=$(($s+$i)) ##变量运算 i=$(($i+1)) done echo " ...
- 【linux】压缩和解压缩
.gz格式 压缩gzip: gzip只能压缩文件,且压缩后文件消失,不能压缩目录. [root@andon tmp]# ls ml orbit-gdm pulse-2sLvu7UbjUYf pulse ...
- centos7开机/etc/rc.local不执行的问题
最近发现centos7 的/etc/rc.local不会开机执行,于是认真看了下/etc/rc.local文件内容的就发现了问题的原因了 #!/bin/bash# THIS FILE IS ADDED ...
- TX Textcontrol 使用总结五——添加图片
实现如图所示效果: 实现代码如下所示: 注意,此处不做代码格式化处理... using System;using System.Collections.Generic;using System.Dra ...
- SQLite加密的方法(c#)
http://blog.csdn.net/xjbx/article/details/2712236 设置下密码就可以了 http://bbs.csdn.net/topics/380018685 编译为 ...
- Visual C++ for Linux Development
原文 https://blogs.msdn.microsoft.com/vcblog/2016/03/30/visual-c-for-linux-development/ Visual C++ fo ...