끄적끄적 코딩
article thumbnail

단어를 뒤집어서 출력하는 문제입니다.

줄단위로 받기위해서 getline을 사용하였으며,
시작점을 지정해놓고 공백이거나 끝일때 시작점부터 공백전까지
substr로 잘라줍니다. 그리고 reverse를 통해서 뒤집어서 출력해주었습니다.

 

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

int main(int argc, char *argv[])
{
	int n= 10;
	int start, end;
	string s;
	string temp;

	cin >> n;
	
	getline(cin, s);
	while (n--) {
		end = 0;
		start = 0;
		getline(cin, s);

		for (int i = 0; i <= s.length(); ++i) {
			if (s[i] == ' ' || i == s.length()) {
				temp = s.substr(start, end);
				reverse(temp.begin(), temp.end());
				cout << temp << " ";
				start = i + 1;
				end = -1;
			}
			++end;
		}
		cout << "\n";
	}

	return 0;
}

검색 태그