给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串

输入:

输入只有一组数据

输入一个短字符串(不含空格),再输入若干字符串直到文件结束为止

输出:

删除输入的短字符串(不区分大小写)并去掉空格

#include <stdio.h>
#include <string>
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
char str[];
gets(str); //输入短字符串
string a = str;
for(int i = ; i<a.size(); ++i)
{ //将a中字符串全部改为小写字母
a[i] = tolower(a[i]);
}
while(gets(str))
{
string b = str,c = b; //将字符串保存至b,c,b将用于保存小写化后的字符,c保存原字符串
for(int i = ; i<b.size(); ++i)
{ //将b中的字符全部改为小写,便于匹配
b[i] = tolower(b[i]);
}
int t = b.find(a,); //在b串中查找a的位置,返回索引
while(t != string::npos) //查找成功,则重复循环
{
c.erase(t,a.size());
b.erase(t,a.size());
t = b.find(a,t); //从t位置为起点继续查找b中下一个出现字符串a的位置
}
t = c.find('',);
while(t != string::npos)
{ //删除c中所有空格
c.erase(t,);
t = c.find(' ',);
}
cout<<c<<endl;
}
return ;
}

可见,在使用了string对象后,关于字符串处理的问题将得到大大的简化,注意,由于要求中“大小写不区分”,所以我们先将字符串全部改为小写后进行匹配,tolower函数,于

C标准函数库中的头文件 ctype.h中

注意:

查找字符串a是否包含子串b,不是用strA.find(strB) > 0 而是 strA.find(strB) != string:npos

其中string:npos是个特殊值,说明查找没有匹配

string::size_type pos = strA.find(strB);
if(pos != string::npos){}
-------------------------------------------
int idx = str.find("abc");
if (idx == string::npos)
...
上述代码中,idx的类型被定义为int,这是错误的,即使定义为 unsigned int 也是错的,它必须定义为 string::size_type。
npos 是这样定义的:
static const size_type npos = -1;

因为 string::size_type (由字符串配置器 allocator 定义) 描述的是 size,故需为无符号整数型别。因为缺省配置器以型别 size_t 作为 size_type,于是 -1 被转换为无符号整数型别,npos 也就成了该型别的最大无符号值。不过实际数值还是取决于型别 size_type 的实际定义。不幸的是这些最大值都不相同。事实上,(unsigned long)-1 和 (unsigned short)-1 不同(前提是两者型别大小不同)。因此,比较式 idx == string::npos 中,如果 idx 的值为-1,由于 idx 和字符串string::npos 型别不同,比较结果可能得到 false。
要想判断 find() 的结果是否为npos,最好的办法是直接比较:

if (str.find("abc") == string::npos) { ... }

错误:if(str.find("abc") ) 
注:找不到abc会返回-1,不为0为True。0为False

通常来说,find函数用于寻找某个序列的在string中第一次出现的位置。

find函数有以下四种重载版本:

size_t find (const string& str, size_t pos = ) const noexcept;
size_t find (const char* s, size_t pos = ) const;
size_t find (const char* s, size_t pos, size_type n) const;
size_t find (char c, size_t pos = ) const noexcept;
第一个参数总是被搜寻的对象。
第二个参数指出string内的搜索起点(索引)。
第三个参数指出搜寻的字符个数。
////find函数返回类型 size_type
string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position; //find 函数 返回jk 在s 中的下标位置
position = s.find("jk");
if (position != s.npos) //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,
{
cout << "position is : " << position << endl;
}
else
{
cout << "Not found the flag" + flag;
} //find 函数 返回flag 中任意字符 在s 中第一次出现的下标位置
flag = "c";
position = s.find_first_of(flag);
cout << "s.find_first_of(flag) is : " << position << endl; //从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标
position=s.find("b",);
cout<<"s.find(b,5) is : "<<position<<endl; //查找s 中flag 出现的所有位置。
flag="a";
position=;
int i=;
while((position=s.find_first_of(flag,position))!=string::npos)
{
//position=s.find_first_of(flag,position);
cout<<"position "<<i<<" : "<<position<<endl;
position++;
i++;
} //查找flag 中与s 第一个不匹配的位置
flag="acb12389efgxyz789";
position=flag.find_first_not_of (s);
cout<<"flag.find_first_not_of (s) :"<<position<<endl; //反向查找,flag 在s 中最后出现的位置
flag="";
position=s.rfind (flag);
cout<<"s.rfind (flag) :"<<position<<endl;
}

字符串的查找删除---C++中string.find()函数与string::npos的更多相关文章

  1. C++中string::find()函数和string::npos函数的使用

    1. string::find()函数和string::npos函数的介绍 我们在学习C++的时候必不可少的使用到string类中的find()函数,它是一个查找函数,功能还是很强大的,但是此处我们不 ...

  2. C++中string.find()函数,string.find_first_of函数与string::npos

    查找字符串a是否包含子串b,不是用strA.find(strB) > 0而是strA.find(strB) != string:nposstring::size_type pos = strA. ...

  3. C++中string.find()函数与string::npos

    先说说string::npos参数: npos 是一个常数,用来表示不存在的位置,类型一般是std::container_type::size_type 许多容器都提供这个东西.取值由实现决定,一般是 ...

  4. 九度OJ 1168:字符串的查找删除 (查找)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4276 解决:1699 题目描述: 给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串. 输入: 输入只有1 ...

  5. C# 中奇妙的函数–8. String Remove() 和 Replace()

    http://www.cnblogs.com/multiplesoftware/archive/2011/09/27/2192710.html 当对字符串进行操作时,我们经常要删除或者是替换一部分子字 ...

  6. C# 中奇妙的函数–7. String Split 和 Join

    很多时候处理字符串数据,比如从文件中读取或者存入 - 我们可能需要加入分隔符(如CSV文件中的逗号),或使用一个分隔符来合并字符串序列. 很多人都知道使用split()的方法,但使用与其对应的Join ...

  7. Go语言中字符串的查找方法小结

    这篇文章主要介绍了Go语言中字符串的查找方法小结,示例的main函数都是导入strings包然后使用其中的方法,需要的朋友可以参考下   1.func Contains(s, substr strin ...

  8. C语言:将ss所指字符串中所有下标为奇数位上的字母转换成大写,若不是字母,则不转换。-删除指针p所指字符串中的所有空白字符(包括制表符,回车符,换行符)-在带头结点的单向链表中,查找数据域中值为ch的结点,找到后通过函数值返回该结点在链表中所处的顺序号,

    //将ss所指字符串中所有下标为奇数位上的字母转换成大写,若不是字母,则不转换. #include <stdio.h> #include <string.h> void fun ...

  9. C语言:假定输入的字符串只包含字母和*号,fun函数:除了尾部的*号以外,将字符的其他*号进行全部删除,形参p已经指向字符串中最后一个字母。-利用折半查找整数m在有序数组中的位置,若找到,返回下标值,否则返回-1。

    //假定输入的字符串只包含字母和*号,fun函数:除了尾部的*号以外,将字符的其他*号进行全部删除,形参p已经指向字符串中最后一个字母. #include <stdio.h> void f ...

随机推荐

  1. html网页自动跳转页面代码

    方案一,用<meta>里直接写刷新语句: <html><head><meta http-equiv="Content-Language" ...

  2. 第14篇 PSR-3规范(日志)

    1. Specification 1.1 Basics The LoggerInterface exposes eight methods to write logs to the eight RFC ...

  3. Tair 分布式K-V存储方案

    tair 是淘宝的一个开源项目,它是一个分布式的key/value结构数据的解决方案. 作为一个分布式系统,Tair由一个中心控制节点(config server)和一系列的服务节点(data ser ...

  4. PHP面向对象深入研究之【命名空间】与【自动加载类】

    命名空间 避免类名重复,而产生错误. <?php require_once "useful/Outputter.php"; class Outputter { // outp ...

  5. git版本控制-详细操作

    - git,软件帮助使用者进行版本的管理 阶段一git 命令: git init 初始化 git config --global user.email "you@example.com&qu ...

  6. swift(Object Storage对象存储服务)(单节点)

    # 在部署对象存储服务(swift)之前,你的环境必须包含身份验证服务(keystone); # keystone需要MySQL数据库,Rabbitmq服务,Memcached服务; # 内存:4G ...

  7. Python Twisted系列教程6:抽象地利用Twisted

    作者:dave@http://krondo.com/and-then-we-took-it-higher/  译者:杨晓伟(采用意译) 你可以从这里从头开始阅读这个系列. 打造可以复用的诗歌下载客户端 ...

  8. left join的多重串联与groupby

    有三张表或组合查询,f1,f2,f3,其中,f1分别与f2,f3是一对多关系,f1一条记录可能对应f2或f3中0条或多条记录 要创建一个查询,以f1为基准,即f1中有多少条记录,结果也就返回对应数量的 ...

  9. [原创]20行ruby代码实现依赖注入框架

    我需要依赖注入 业余时间开发的娱乐项目 (为了练习使用ruby语言) 遵循SRP原则,业务逻辑拆分由各个service类型提供,假设存在如下几个类型 GameService 封装主要游戏业务逻辑 Us ...

  10. ubuntu 12.04的源更新

    apt-get install vim 安装vim vim /etc/apt/sources.list deb http://mirrors.163.com/ubuntu/ precise main ...