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

输入:

输入只有一组数据

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

输出:

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

#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. 用Toast来增加调试效率的小技巧

    import android.content.Context; import android.widget.Toast; /** * Created by apple on 10/7/15. */ p ...

  2. redis1

    1. redis持久化有两种方式 ① RDB:就是周期性(比如5s)将内存中的数据放到硬盘 ② AOF:就是增删改操作写入日志,根据日志恢复等 2. redis三种分布式 ①主从 ②哨兵 ③集群 3. ...

  3. 别人的dubbo学习笔记

    本文转载自:http://blog.csdn.net/tao_qq/article/details/49952229 学习dubbo,开始做一些笔记. 1> 启动dubbo-admin模块的时候 ...

  4. 批处理判断是否有.net环境

    @echo off (echo 已安装.NET Framework) else (echo 未安装.NET Framework) pause>nul

  5. Quartz.net 2.x 学习笔记02-Quartz.net 2.x在MVC站点中结合Log4net的使用

    Quartz.net 2.x在MVC站点中结合Log4net的使用 首先新建一个MVC的空站点: 第二步,添加Quartz.net的引用 在搜索处输入quartz.net搜索安装即可(目前是2.3) ...

  6. mysql实战优化之六:Order by优化 sql优化、索引优化

    在MySQL中的ORDER BY有两种排序实现方式: 1.利用有序索引获取有序数据 2.文件排序 在使用explain分析查询的时候,利用有序索引获取有序数据显示Using index.而文件排序显示 ...

  7. 五 搭建kafka集群

    1 下载    wget http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.0.0/kafka_2.12-2.0.0.tgz 2 tar  -zxv ...

  8. jQuery笔记——基础知识

    jQuery是一个JavaScript库,它通过封装原生的JavaScript函数得到一整套定义好的方法.在jQuery程序中,不管是页面元素的选择.内置的功能函数,都是美元符号“$”来起 始的.而这 ...

  9. C Primer Plus学习笔记(十)- 字符串和字符串函数

    getchar() 和 putchar() getchar() 函数不带任何参数,它从输入队列中返回下一个字符 下面的语句读取下一个字符输入,并把该字符的值赋给变量 ch ch =getchar(); ...

  10. Python——List

    一.集成开发环境 集成开发环境(IDE,Integrated development Enviroment)是用于提供程序开发环境的应用程序,一般包括代码编辑器.编译器.调试器和图形用户界面等工具.集 ...