0
点赞
收藏
分享

微信扫一扫

vulnhub靶机:21 LTR: Scene1

程序小小黑 2024-09-19 阅读 22

问题:

解答:
main.cpp

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

bool palindromic(string& s);

int main()
{
	string st;
	cout << "Enter the string to test: ";
	getline(cin, st);
	cout << "String " << st << " is ";
	if (palindromic(st))
	{
		cout << "a palindromic string. " << endl;
	}
	else
	{
		cout << "not a palindromic string." << endl;
	}

	

	return 0;
}

bool palindromic(string& s)
{
	auto phead = s.begin();
	auto ptail = s.end();
	while (ptail>phead)
	{
		if (!isalpha(*phead))
		{
			phead++;
			continue;
		}
		if (!isalpha(*(ptail-1)))
		{
			ptail--;
			continue;
		}
		if (toupper(*phead) == toupper(*(ptail-1)))
		{
			phead++;
			ptail--;
		}
		else
		{
			return false;
		}
	}
	return true;
}

运行结果:
在这里插入图片描述

考查点:

  • 迭代器
  • cctype函数

注意:

  • 迭代器end()不是最后一个,是最后一个的下一个位置.
    在这里插入图片描述

2024年9月13日21:38:06

举报

相关推荐

0 条评论