逆置字符串

逆置字符串
问题描述:将字符串中的元素按单词逆序,标点符号除外。
解决方法:首先将整个字符串逆序,使用reverse,然后将每个单词进行reverse,就得到想要的结果了。

#include <iostream>
#include <string>
using namespace std;


int main()
{
	string str1;
	getline(cin, str1);
	reverse(str1.begin(), str1.end());
	auto start = str1.begin();
	while (start != str1.end())
	{
		auto end = start;
		while (end != str1.end() && *end != ' ')
			++end;
		reverse(start, end);

		if (end == str1.end())
			start = end;
		else
			start = end + 1;
	}
	cout << str1 << endl;
	return 0;
}

发布了149 篇原创文章 · 获赞 27 · 访问量 5057

猜你喜欢

转载自blog.csdn.net/qq_44783220/article/details/102992134
今日推荐