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中常见的字符串操作函数及用法 最近几次参加前端实习生招聘的笔试,发现很多笔试题都会考到字符串的处理,比方说去哪儿网笔试题.淘宝的笔试题等.如果你经常参加笔试或者也是一个过来人,相信 ...
随机推荐
- Java实体书写规范
** * 用户角色表 */ public class BaseUserRole implements Serializable { private static final long serialVe ...
- poj -2229 Sumsets (dp)
http://poj.org/problem?id=2229 题意很简单就是给你一个数n,然后选2的整数幂之和去组成这个数.问你不同方案数之和是多少? n很大,所以输出后9位即可. dp[i] 表示组 ...
- [HDOJ2830]Matrix Swapping II(胡搞)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2830 给一个矩阵只有0和1,矩阵的列可以和其他列交换无数次,问交换后整个矩阵形成的最大的全是1的子矩阵 ...
- hibernate工具类HibernateUtil详解
1.为什么要用hibernateUtil这个类,先看这段代码: //加载配置文件信息默认为hiberna.cfg.xml,如果不是的话那么就在config()方法里面去解析他 Con ...
- ACM - ICPC World Finals 2013 H Матрёшка
原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 俄罗斯套娃是一些从外到里大小递减的传 ...
- R语言实现数据集某一列的频数统计——with和table
with(priority.train, table(From.EMail)) 统计priority.train中From.EMail的频数
- Machine Learning for hackers读书笔记(二)数据分析
#均值:总和/长度 mean() #中位数:将数列排序,若个数为奇数,取排好序数列中间的值.若个数为偶数,取排好序数列中间两个数的平均值 median() #R语言中没有众数函数 #分位数 quant ...
- eclipse中字母大小写转换快捷键
ctrl+shift+x 转为大写 ctrl+shift+y 转为小写
- 引用问题rayshop.common总是提醒重复引用问题
引用问题rayshop.common总是提醒重复引用问题 解决办法:在插件组里的所有项目引用,使用不能复制属性
- 51nod1274 最长递增路径
将边排序后dp一下就可以了. #include<cstdio> #include<cstring> #include<cctype> #include<alg ...