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中常见的字符串操作函数及用法 最近几次参加前端实习生招聘的笔试,发现很多笔试题都会考到字符串的处理,比方说去哪儿网笔试题.淘宝的笔试题等.如果你经常参加笔试或者也是一个过来人,相信 ...
随机推荐
- 【周全考虑】CodeForces 245B——Internet Address
来源:点击打开链接 看上去很简单的一道题,可是错的次数却不少. 题目要求是将一串字母转化成网址——形如格式http(ftp)://xxx.ru/xxxx的样子,看上去很简单,可是还是很容易出错. 刚开 ...
- 如何在User版本开启串口(Uart),抓取上层Log,开启输入控制台
[原][FAQ03891] 如何在User版本开启串口(Uart),抓取上层Log,开启输入控制台 2014-11-26阅读1369 评论0 FAQ Content [Description]如何在U ...
- build.gradle(Project) 和 build.gradle(Module) 的区别
参考: http://stackoverflow.com/questions/28295933/difference-between-build-gradleproject-and-build-gra ...
- How do I install uudecode?
uudecode is in the package sharutils Install it with the package manager or start a terminal and typ ...
- 利用XPath读取Xml文件
之所以要引入XPath的概念,目的就是为了在匹配XML文档结构树时能够准确地找到某一个节点元素.可以把XPath比作文件管理路径:通过文件管理路 径,可以按照一定的规则查找到所需要的文件:同样,依据X ...
- Linux同步机制 - 多线程开发总结
1 对于CPU开销大的场景,能利用多核,就尽量利用多核(常常自以为某需求的运算量不大,且CPU足够快,就偷懒写个单线程,结果效率很低) 2 使用多线程的时候,默认是加锁的.在加锁保证业务正常的条件下, ...
- Codeforces Round #215 (Div. 1) B
出来冒个泡 由于数比较大 开了map计数 然后边走边删边加 勉强可过 #include <iostream> #include<cstdio> #include<cs ...
- MFC弹出菜单隐藏解决
http://social.msdn.microsoft.com/Forums/en-US/5482103e-272b-4c9f-bac4-be15f14782bd/cmfcmenubar-remov ...
- 【JavaScript学习笔记】鼠标样式
style="cursor:hand" 手形 style="cursor:crosshair" 十字形 style="cursor ...
- impdp之remap_schema参数
众所周知,IMP工具的FROMUSER和TOUSER参数可以实现将一个用户的的数据迁移到另外一个用户.同样的功能在IMPPDP工具中如何得以体现呢?答案就是:使用IMPPDP的REMAP_SCHEMA ...