T1103——统计数字字符个数
代码如下——
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
int cnt = 0;
getline(cin, str);
for (auto ch : str)
{
if (ch >= '0' && ch <= '9')
{
cnt++;
}
}
cout<<cnt;
return 0;
}
运行结果如下——
T1104——找第一个只出现一次的字符
代码如下——
#include<iostream>
#include<string>
using namespace std;
int vis[30];
int main()
{
string str;
cin>>str;
for (int i=0;i<str.size();++i)
{
vis[str[i] - 96]++;
}
bool f = false;
int sub = 0;
for (int i = 0; i < str.size(); ++i)
{
if (vis[str[i] - 96] == 1)
{
f = true;
sub = i;
break;
}
}
if (f == true)cout << str[sub] << endl;
else cout << "no" << endl;
return 0;
}
T1109——字符替换
代码如下——
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s, newStr; char c,d; cin >> s >> c >> d;
for (auto ch : s){
if (ch != c) newStr += ch;
else if (ch == c )
{
newStr += d;
}
else if (ch == c )newStr += c;
}
cout << newStr << endl;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s, c, d; cin >> s >> c >> d;
int sub = 0;
while ((sub = s.find(c, sub)) != s.npos)
{
s.replace(sub, 1, d);
}
cout << s << endl;
}
T1110——密码翻译
代码如下——
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str, newstr; getline(cin, str);
for (auto ch : str)
{
if (ch == 'Z') newstr+= 'A';
else if (ch == 'z') newstr += 'a';
else if ((ch >= 'a' && ch < 'z') || (ch >= 'A' && ch < 'Z')) newstr += (ch + 1);
else newstr += ch;
}
cout << newstr << endl;
}
T1829——二进制转化为十进制****************
代码如下——
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n, sum = 0; string str; cin >> n >> str;
if (str[n - 1] == '1') sum += 1;
for (int i = n - 2, j = 1; i >= 0; --i, j++)if (str[i] == '1') sum += 2 << (j - 1);
cout << sum << endl;
}
T1114——忽略大小写的字符串比较
代码如下——
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a, b; cin >> a >> b;
for (int i = 0; i < a.size() || i < b.size(); ++i)
{
if(i<a.size() && a[i]<'a') a[i]=tolower(a[i]);
if(i<b.size() && b[i]<'a') b[i]=tolower(b[i]);
}
if (a > b) cout << ">" << endl;
else if (a < b) cout << "<" << endl;
else cout << "=" << endl;
}
T1115——字符串判等
代码如下——复制黏贴的时候要改的东西一定要注意!!!!!!!!!!
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a, b; getline(cin, a); getline(cin, b);
string tempA = "";string tempB = "";
for (size_t i = 0; i < a.size(); ++i)if (a[i] != ' ') tempA += a[i];
for (size_t i = 0; i < b.size(); ++i)if (b[i] != ' ')tempB += b[i];
a = tempA;b = tempB;
for (size_t i = 0; i < a.size() || i < b.size(); ++i)
{
if(i<a.size() && a[i]<'a') a[i]=tolower(a[i]);
if(i<b.size() && b[i]<'a') b[i]=tolower(b[i]);
}
if (a == b)cout << "YES" << endl;
else cout << "NO" << endl;
}