#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; }
时间: 2024-10-25 12:33:46