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() 函数的更多相关文章

  1. (转载)C++ string中find() ,rfind() 等函数 用法总结及示例

    string中 find()的应用  (rfind() 类似,只是从反向查找) 原型如下: (1)size_t find (const string& str, size_t pos = 0) ...

  2. 【模板】string中substr函数的运用

    substr有两种用法: 假设:string s = "0123456789" ;  //下标从0开始 ① string a = s.substr(5)               ...

  3. string中常用的函数

    string中常用的函数 发现在string在处理这符串是很好用,就找了一篇文章放在这里了.. 用 string来代替char * 数组,使用sort排序算法来排序,用unique 函数来去重1.De ...

  4. mysql中的substr()函数

    mysql中的substr()函数和hibernate的substr()参数都一样,就是含义有所不同. 用法: substr(string string,num start,num length); ...

  5. Oracle中的substr()函数 详解及应用

    注:本文来源于<Oracle中的substr()函数 详解及应用> 1)substr函数格式   (俗称:字符截取函数) 格式1: substr(string string, int a, ...

  6. C++string中有关字符串内容修改和替换的函数浅析

    1.assign() 原型: //string (1) basic_string& assign (const basic_string& str); //substring (2) ...

  7. C++ string中的find()函数

    1.string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos.(返回值可以看成是一个int型的数) #include<cstring> ...

  8. string 中的 length函数 和size函数 返回值问题

    string 中的 length函数 和 size函数 的返回值  (  还有 char [ ] 中 测量字符串的  strlen 函数 ) 应该是 unsigned int 类型的 不可以 和 -1 ...

  9. JavaScript中常见的字符串操作函数及用法

    JavaScript中常见的字符串操作函数及用法 最近几次参加前端实习生招聘的笔试,发现很多笔试题都会考到字符串的处理,比方说去哪儿网笔试题.淘宝的笔试题等.如果你经常参加笔试或者也是一个过来人,相信 ...

随机推荐

  1. Data Base sqlServer基础知识

    sqlServer   基础知识 大纲 创建数据库 1 创建表 2 备份表 3 删除表 4 修改表 5 查询出重复的数据 6 增删改查 7 添加约束 8 分页存储过程 9 排序 10 类型转换 11 ...

  2. rqnoj-106-最大加权矩形-dp

    和我之前做的那个切西瓜的题目相比就是小巫见大巫了.. 运用最长字段和的原理把O(n^4)转化成O(n^3) #include<stdio.h> #include<string.h&g ...

  3. asp.net mvc4使用百度ueditor编辑器

    原文  http://www.cnblogs.com/flykai/p/3285307.html    已测试 相当不错 前言 配置.net mvc4项目使用ueditor编辑器,在配置过程中遇见了好 ...

  4. Vim 新用法

    daw , delete a word cw , delete from cursor to the end then insert mode a word 移动: f ; Aa Oo Cc Ii S ...

  5. RSA (cryptosystem)

    https://en.wikipedia.org/wiki/RSA_(cryptosystem) RSA is one of the first practical实用性的 public-key cr ...

  6. delete-node-in-a-bst

    https://leetcode.com/problems/delete-node-in-a-bst/ /** * Definition for a binary tree node. * struc ...

  7. ArcGis Javascript API (V3.6)加载天地图

    Arcgis的Javascript api开发很活跃,不知不觉都发布了3.6的版本了.该版本基于dojo 1.8.3开发的. 从dojo 1.8开始,AMD机制用得越来越多了,而且require([& ...

  8. laravel中的$request对象构造及请求生命周期

    laravel应用程序中index.php是所有请求的入口.当用户提交一个form或者访问一个网页时,首先由kernel捕捉到该session PHP运行环境下的用户数据, 生成一个request对象 ...

  9. AngularJS分页实现

    基本思路 一开始页码为1,Service向服务器端获取对应信息:点击上/下一页/跳转,通过对应的页码向服务器端获取对应的信息. 由于后台暂时没弄好,我实现的过程中直接读取准备好的JSON文件,通过页码 ...

  10. [Mac][MySQL]如何启动MySQL Server

    方法来自 MySQL 5.7官方手册 http://dev.mysql.com/doc/refman/5.7/en/osx-installation-launchd.html 有两种方法,另一种是命令 ...