SP1163 JAVAC - Java vs C &#43&#43

传送门:SP1163 JAVAC - Java vs C &#43&#43

很明显一字符串模拟题,但是要考虑几个问题,这是翻译没有写的,首先如果 _ 后面还是 _,要输出 Error!;第二,如果字符串末尾或第一个是 _,也要输出 Error!;最后,如果字符串第一个是大写,也要输出 Error!

然后就是模拟了:

#include <bits/stdc++.h>
using namespace std;

int main()
{
	string s;
	while (cin >> s)
	{
		if (s[0] == '_' || isupper(s[0]) || s.back() == '_')
		{
			cout << "Error!" << endl;
			continue;
		}
		int len = s.length() - 1;
		string copy = s;
		transform(copy.begin(), copy.end(), copy.begin(), ::tolower);
		int fd = s.find("_");
		string finalans = "";
		if (fd != string::npos && copy != s)
		{
			cout << "Error!\n";
			continue;
		}
		if (fd != string::npos)
		{
			for (int i = 0; i <= len; i++)
			{
				if (s[i] == '_')
				{
					if (isupper(s[i + 1]) || s[i + 1] == '_')
					{
						finalans = "Error!";
						break;
					}
					else
					{
						s[i + 1] = toupper(s[i + 1]);
					}
				}
				else
				{
					finalans += s[i];
				}
			}
		}
		else
		{
			for (int i = 0; i <= len; i++)
			{
				if (isupper(s[i]))
				{
					finalans += "_";
					s[i] = tolower(s[i]);
				}
				finalans += s[i];
			}
		}
		cout << finalans << endl;
	}
	return 0;
}
posted @   HappyBobb  阅读(3)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示