您的位置:首页 > 其它

【华为OJ】1. 输入字符串和一个字符,找到相同字符个数,不区分大小写!!!

2014-06-30 22:24 513 查看
#include<iostream>
#include<string>
using namespace std;
int main()
{
         char a[1000],s1;
         int count=0;
         string s;
         getline(cin,s);
         strcpy(a,s.c_str());//字符串复制,c_str返回当前字符串首字符地址。
     cin>>s1;
          for(int i=0;i<s.size();++i)
         {
         if(toupper(s1)==toupper(a[i]))//转换为大写
                   count++;
         }
         cout<<count<<endl;
         system("pause");
    return 0;
}

=======================================
舟学霸的
#include<iostream>
#include<string.h>
using namespace std;
int main(){
         char str[1000];
    char sh[1000];
    char* result;
    int n = 0;
    cin.getline(str, 1000);
    cin.getline(sh,1000);
         for(int i=0;i<1000;i++){
        str[i] = tolower(str[i]);
                   sh[i] = tolower(sh[i]);
         }
    for (result = strstr(str, sh);result&&++n;)
        (result = strstr(result+1,sh));
    cout << n << endl;
         system("pause");
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐