처음 작성한 코드는 다음과 같으나 틀렸다는 결과가 나왔다.
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
// 공백을 없애는 함수
string removeSpaces(const string& input) {
string result;
for (char c : input) {
if (!isspace(static_cast<unsigned char>(c))) {
result += c;
}
}
return result;
}
// 쉼표를 기준으로 문자열을 분할하는 함수
vector<string> splitByComma(const string& input) {
vector<string> result;
istringstream iss(input);
string token;
while (getline(iss, token, ',')) {
result.push_back(token);
}
return result;
}
int main(){
string first;
string str;
cin >> first >> str;
// 공백을 없애고 쉼표로 문자열을 분할
str = str.substr(0, str.length()-1);
string stringWithoutSpaces = removeSpaces(str);
vector<string> splitResult = splitByComma(stringWithoutSpaces);
//출력
int n;
for(int i=0; i<splitResult.size(); i++){
cout << first;
for(int j=splitResult[i].size()-1; j>=0; j++){
if( (65<=splitResult[i][j] && splitResult[i][j]<=90) ||
(97<=splitResult[i][j] && splitResult[i][j]<=122) ) {
n = j;
break;
}
else {
cout << splitResult[i][j];
}
}
cout << " ";
for(int j=0; j<=n; j++)
cout << splitResult[i][j];
cout << ";";
cout << "\n";
}
return 0;
}
대충 생각한 논리는 맞으나
string을 입력받아 ',"를 기준으로 split해 다른 string으로 저장해주는 부분 등이 c++로는 구현하기 어려운 부분이 문제였다.
파이썬 del, split 함수, list등을 쓰면 쉽게 구현할 수 있는 문제였다.
'백준' 카테고리의 다른 글
백준 2290 LCD Test, c++ (0) | 2024.03.08 |
---|---|
백준 16506 CPU, c++ (1) | 2024.03.07 |
백준 1141 접두사, c++ (0) | 2024.03.06 |
백준 12026 BOJ 거리, c++ (0) | 2024.03.06 |
백준 1495 기타리스트, c++ (0) | 2024.03.05 |