首页 > 编程语言 > 详细

2015.09.09 C++笔记

时间:2015-09-10 19:05:14      阅读:223      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <string>
using namespace std;


int count( const string &s, char ch, int &index )
{
    index = s.size();
    int counter = 0;
    for (int i = 0; i<s.size()+1; i++)
    {
        if (s[i]==ch)
        {
            if (index == s.size()+1)index = i+1;
            counter++;
        }
    }
    return counter;
}

int main()
{
    //02.返回字符串中某字符的数量和首次出现的位置,若没有则返回0和字符串长度
    string s; char ch;
    cin>>s;
    cin>>ch;
    int counter, index;
    counter = count(s, ch, index);
    cout<<counter<<" "<<index<<endl;


    //01.提示输入密码,如果两次输入不一致则报错。使用try, throw和catch
    string s1, s2;
    try
    {
        cout<<"Please enter password:"<<endl;
        cin>>s1;
        cout<<"\nPlease enter again:"<<endl;
        cin>>s2;
        if (s1 != s2)
            throw runtime_error("wrong password!");
    }
    catch (runtime_error err)
    {
        cerr<<err.what()<<endl;
    }

    cin>>s1;
    return 0;
}

 

2015.09.09 C++笔记

原文:http://www.cnblogs.com/wangzhuazhua/p/4798682.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!