string容器迭代器使用案例

//用户博客用户名验证

//判断博客用户名的有效性
//判断用户输入的用户名是否包含除了小写字母之外的字符

//判断用户输入的博客用户名是否正确

#include<iostream>

#include<string>

using namespace std;

int main()

{
    
    string name = "abcd342@hot.j";
    
    int pos1 = name.find("@");
    
    int pos2 = name.find(".");
    
    if (pos1 == -1 || pos2 == -1)
        
    {
        cout << "输入的博客用户名不包含@或者." << endl;
        system("pause");
        return 0;
        
    }
    if (pos1 > pos2)
    { 
        cout << "输入的博客用户名不合法!" << endl;
    }
    string username = name.substr(0, pos1);
    //拿迭代器
    
    string::iterator pStart = username.begin();
    string::iterator pEnd = username.end();
    while (pStart != pEnd)
    {
        if (*pStart <=97 || *pStart > 122)
        {
            cout << "输入的字符包含非小写字符!" << endl;
            return 0;
        }
        pStart++;
    }

    cout << endl;
    //比较邮箱
    string rightemail = "kjlk@itcast.cn";
    int ret = name.compare(rightemail);
    if (ret != 0)
    {
        cout << "博客用户名存在" << endl;
        system("pause");
    }
    cout << "博客用户名正确";

        system("pause");
    return EXIT_SUCCESS;
}



#C/C++#
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务