zoukankan      html  css  js  c++  java
  • 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 <=8)
            return false;
         else 
            return true;
    }
    
    
    bool checkKind(string& pwd)
    {
         int upCase = 0;
         int lowCase = 0;
         int other = 0;
         int dight = 0;
            
            for(unsigned int i = 0; i < pwd.length(); i++)
            {
                if(pwd[i] >= 'a' && pwd[i] <='z')
                {
                    lowCase = 1;
                    continue;
                }   
                else if(pwd[i] >= 'A' && pwd[i] <='Z')
                {
                    upCase = 1;
                    continue;
                }    
                else if(pwd[i] >= '0' && pwd[i] <='9')
                {
                    dight = 1;
                    continue;
                }    
                else 
                {
                    other++;
                    continue;
                }
            }
            if(upCase + lowCase + dight + other < 3)
                return false;
            else 
                return true;
    }
    
    
    bool checkRepeat(string& pwd)
    {
         for(unsigned int i = 0; i < pwd.length() - 2;i++)
         {
             string substr1 = pwd.substr(i,i + 3);
             for(unsigned int j = i + 1; j < pwd.length() - 2; j++)
             {
                 string substr2 = pwd.substr(j);
                 string::size_type pos = 0;
                 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;
        }
    }
    密码检查
     
  • 相关阅读:
    Java 中常用的数据源
    Restful风格API接口开发springMVC篇
    Maven的作用到底是什么
    Java中常用的数据源
    数据库死锁问题 及 解决方法
    string中执行sql语句
    提高mysql千万级大数据SQL查询优化30条经验
    JAVA对象转换JSON
    oracle分页查询
    jdk8环境变量 jdk8图解安装 java8安装
  • 原文地址:https://www.cnblogs.com/lp3318/p/5587808.html
Copyright © 2011-2022 走看看