string中的substr() 和 find() 函数
string问题中经常遇到在stringA中查找stringB,主要通过substr()跟find()来完成
substr()、find()、replace() 都可以用一个位置加上一个长读去描述子串,substr()用于读字符串,replace()用于写字符串
1.find():
int find(char c, int pos = 0) const; //从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const; //从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const; //从pos开始查找字符串s中前n个字符在当前串中的位置(n 为参数中*s的要查找 的前n个字符数)
查找成功返回查找索引,失败则返回string::npos;
2.substr()
返回一个从指定位置开始,并具有指定长度的子字符串
string substr (size_t pos = 0, size_t len = npos) const; //从当前串中复制 pos开始 长度为len的子串并返回
pos: 如果pos大于string.length() ,抛出out_of_range,
len: 要取得的子串长度
例子:密码识别
/*
备注:牛客网华为笔试题,70%通过,实在没法改了
*/ #include<iostream>
#include<string> using namespace std; bool checkLen(string &pwd)
{
int len = pwd.length();
if(len <=)
return false;
else
return true;
} bool checkKind(string& pwd)
{
int upCase = ;
int lowCase = ;
int other = ;
int dight = ; for(unsigned int i = ; i < pwd.length(); i++)
{
if(pwd[i] >= 'a' && pwd[i] <='z')
{
lowCase = ;
continue;
}
else if(pwd[i] >= 'A' && pwd[i] <='Z')
{
upCase = ;
continue;
}
else if(pwd[i] >= '' && pwd[i] <='')
{
dight = ;
continue;
}
else
{
other++;
continue;
}
}
if(upCase + lowCase + dight + other < )
return false;
else
return true;
} bool checkRepeat(string& pwd)
{
for(unsigned int i = ; i < pwd.length() - ;i++)
{
string substr1 = pwd.substr(i,i + );
for(unsigned int j = i + ; j < pwd.length() - ; j++)
{
string substr2 = pwd.substr(j);
string::size_type pos = ;
if((pos = substr2.find(substr1)) != string::npos)
return false;
}
}
return true;
} int main()
{
string pwd;
while(getline(cin,pwd))
{
if(checkLen(pwd) && checkKind(pwd) && checkRepeat(pwd))
cout<<"OK"<<endl;
else
cout<<"NG"<<endl;
}
}
密码检查
string中的substr() 和 find() 函数的更多相关文章
- (转载)C++ string中find() ,rfind() 等函数 用法总结及示例
string中 find()的应用 (rfind() 类似,只是从反向查找) 原型如下: (1)size_t find (const string& str, size_t pos = 0) ...
- 【模板】string中substr函数的运用
substr有两种用法: 假设:string s = "0123456789" ; //下标从0开始 ① string a = s.substr(5) ...
- string中常用的函数
string中常用的函数 发现在string在处理这符串是很好用,就找了一篇文章放在这里了.. 用 string来代替char * 数组,使用sort排序算法来排序,用unique 函数来去重1.De ...
- mysql中的substr()函数
mysql中的substr()函数和hibernate的substr()参数都一样,就是含义有所不同. 用法: substr(string string,num start,num length); ...
- Oracle中的substr()函数 详解及应用
注:本文来源于<Oracle中的substr()函数 详解及应用> 1)substr函数格式 (俗称:字符截取函数) 格式1: substr(string string, int a, ...
- C++string中有关字符串内容修改和替换的函数浅析
1.assign() 原型: //string (1) basic_string& assign (const basic_string& str); //substring (2) ...
- C++ string中的find()函数
1.string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos.(返回值可以看成是一个int型的数) #include<cstring> ...
- string 中的 length函数 和size函数 返回值问题
string 中的 length函数 和 size函数 的返回值 ( 还有 char [ ] 中 测量字符串的 strlen 函数 ) 应该是 unsigned int 类型的 不可以 和 -1 ...
- JavaScript中常见的字符串操作函数及用法
JavaScript中常见的字符串操作函数及用法 最近几次参加前端实习生招聘的笔试,发现很多笔试题都会考到字符串的处理,比方说去哪儿网笔试题.淘宝的笔试题等.如果你经常参加笔试或者也是一个过来人,相信 ...
随机推荐
- A Bug
A Bug Time Limit: 1000MS Memory Limit: 65535KB Submissions: 231 Accepted: Descr ...
- C#高级编程(第9版) -C#5.0&.Net4.5.1 书上的示例代码下载链接
http://www.wrox.com/WileyCDA/WroxTitle/Professional-C-5-0-and-NET-4-5-1.productCd-1118833031,descCd- ...
- [NYIST32]组合数(状压,枚举,暴力)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=32 求n个数中挑出r个数字的所有情况,最后倒序输出所有情况. 状压枚举所有情况就是了 ...
- JSON 之 SuperObject(2): 构建方式与 AsJSon
SuperObject 构建一个 JSON 的常用方法: 从字符串.从文件.从流. unit Unit1; interface uses Windows, Messages, SysUtils, ...
- 【笨嘴拙舌WINDOWS】GDI对象之位图
GDI对象在windows中可以描述成为一次绘画操作时可使用的抽象工具.包括(画笔.画刷.字体.区域.调色板.位图等) GDI对象里的对象概念和在编程领域中面向对象编程的对象概念是不一样的! GDI对 ...
- Qt之QHostInfo
简述 QHostInfo 类为主机名查找提供了静态函数. QHostInfo 利用操作系统提供的查询机制来查询与特定主机名相关联的主机的 IP 地址,或者与一个IP地址相关联的主机名.这个类提供了两个 ...
- UVa 11636 (注意读题) Hello World!
这道题巨坑啊,样例中以-1结束输入的,所以我就天真的以为测试数据也是以-1结束输入的 其实人家原文中说: Input is terminated by a line containing a nega ...
- [swustoj 243] 又是一年CET46
又是一年CET46(0243) 问题描述 CET46 成绩出来啦,一群学生在谈论他们的成绩.A说他的成绩比B高,B说他的成绩比C低,D说他的成绩和E一样…… 他们当中可能有人在说谎.你的任务就是判断是 ...
- (转) error: linker command failed with exit code 1 (use -v to see invocation)
转自:http://blog.csdn.net/tiantian1980/article/details/9175777 像这样的一大堆,总体说编译链接时错误 /Users/zhangtianji ...
- UML和模式应用
引言 Applying UML and Patterns,以一个商店POS系统NextGen和一个掷骰子游戏Monopoly为例,围绕OOA/D的基本原则GRASP,以迭代作为基本方法.以UML为表达 ...