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中常见的字符串操作函数及用法 最近几次参加前端实习生招聘的笔试,发现很多笔试题都会考到字符串的处理,比方说去哪儿网笔试题.淘宝的笔试题等.如果你经常参加笔试或者也是一个过来人,相信 ...
随机推荐
- ubuntu下安装与测试mysql
1.在决定安装mysql之前,要先确定系统是否已经安装mysql. 输入: 1 mysql 结果:说明尚未安装mysql The program 'mysql' is currently notins ...
- (原创)3.2 AddOwner和OverrideMetadata的区别
1 AddOwner和OverrideMetadata 1.1 分析 从源代码上看,AddOwner函数中调用了OverrideMetadata, 并且把本类和依赖属性的哈希值加入到依赖属性的一张哈希 ...
- git环境搭建
Linux kernel 的官方 GIT地址是: http://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git 可以从这个地 ...
- [51NOD1181]质数中的质数(质数筛法)(欧拉筛)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1181 思路:欧拉筛出所有素数和一个数的判定,找到大于n的最小质 ...
- [CF580B]Kefa and Company(滑动窗口)
题目链接:http://codeforces.com/problemset/problem/580/B 某人有n个朋友,这n个朋友有钱数m和关系s两个属性.问如何选择朋友,使得这些朋友之间s最大差距小 ...
- Hadoop集群(第10期副刊)_常用MySQL数据库命令
1.系统管理 1.1 连接MySQL 格式: mysql -h主机地址 -u用户名 -p用户密码 举例: 例1:连接到本机上的MySQL. 首先在打开DOS窗口,然后进入目录 mysqlbin,再键入 ...
- UC编程之网络通信(TCP/UDP)
网络常识 OSI 7层模型(人机交互) 物理层.数据链路层.网络层.传输层.会话层.表现层.应用层 常见协议: tcp/udp/ip/ftp/http... IP地址--就是计算机在网络中的地址,是一 ...
- dotnet il editor 调试 iis 程序
没有C#源代码,IL级别调试.听说windbg也可以,不过windbg有些难.另外il其实一般写C#程序也不熟,不过我目的只是找出异常点,到客户一般不发pdb文件,出去也是release版本,出异常( ...
- Javascript零散知识点总结
一.Javascript获取系统当前时间 <script type="text/javascript"> var time = new Date(); var date ...
- Spring 实践 -AOP
Spring 实践 标签: Java与设计模式 AOP引介 AOP(Aspect Oriented Programing)面向切面编程采用横向抽取机制,以取代传统的纵向继承体系的重复性代码(如性能监控 ...