23-24C++(55)JSK字符串习题(ACM向)二进制转化为十进制(位运算)

 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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拔刀能留住落樱吗、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值