
이 코드는 문제의 테스트 케이스는 통과하지만 제출하면 일부 실패가 뜬다.
일단 다른 풀이로 답을 맞추긴 했지만 왜 틀린 건지 생각해 봤다.
#include <string>
#include <vector>
#include <sstream>
using namespace std;
string solution(string s) {
string answer = "", token;
vector<string> cut;
stringstream str(s);
while (str >> token) {
cut.push_back(token);
}
for (int i = 0; i < cut.size(); i++)
{
for (int j = 0; j < cut[i].size(); j++)
{
if (0 == j % 2) // 0, 짝수 : 대문자
{
cut[i][j] = toupper(cut[i][j]);
}
else // 홀수 : 소문자
{
cut[i][j] = tolower(cut[i][j]);
}
}
answer += cut[i];
if (cut.size()-1 != i)
answer += ' ';
}
return answer;
}

하나 이상의 공백문자라고 써져 있다. 문제를 잘 읽자..
#include <string>
#include <vector>
#include <iostream>
using namespace std;
string solution(string s) {
int idx = 0;
for (int i = 0; i < s.length(); i++)
{
// 공백 문자가 나오면 각 단어의 인덱스 초기화
if (' ' == s[i])
{
idx = 0;
continue;
}
// 인덱스가 0,짝수 : 대문자 , 홀수 : 소문자 변환
s[i] = (0 == idx % 2) ? toupper(s[i]) : tolower(s[i]);
idx++;
}
return s;
}
// C
#include <ctype.h>
// C++
#include <cctype>