인공지능 시대, 코딩은 선택이 아닌 생존 전략입니다

코딩은 미래를 지배하는 기술의 언어, 당신의 가능성을 열어줄 열쇠입니다.

자격증/COS Pro

COSPro 2급 C++] 1차 문제8) 팰린드롬 판단하기 - C++

파아란기쁨1 2022. 7. 12. 18:42
반응형

https://edu.goorm.io/learn/lecture/17165/cos-pro-2%EA%B8%89-%EA%B8%B0%EC%B6%9C%EB%AC%B8%EC%A0%9C-c/lesson/829806/1%EC%B0%A8-%EB%AC%B8%EC%A0%9C8-%ED%8C%B0%EB%A6%B0%EB%93%9C%EB%A1%AC-%ED%8C%90%EB%8B%A8%ED%95%98%EA%B8%B0-c

#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool solution(string sentence) {
    
    string alphas = "";
    for (char ch : sentence) {
        if (ch != '.' && ch != ' ') alphas += ch;
    }
    int len = alphas.size();
    for (int i = 0; i < len / 2; i++) {
        if (alphas[i] != alphas[len - 1 - i]) return false;
    }
    return true;
}

// 아래는 테스트케이스 출력을 해보기 위한 main 함수입니다.
// 아래의 main 함수에서는 틀린 부분 없습니다. 
// solution 함수를 수정하세요.
int main() {
    string sentence1 = "never odd or even.";
    bool ret1 = solution(sentence1);
    
    // Press Run button to receive output.
    cout << "solution 함수의 반환 값은 " << (ret1 == true ? "true" : "false") << " 입니다." << endl;
    

    string sentence2 = "palindrome";
    bool ret2 = solution(sentence2);
    
    // Press Run button to receive output.
    cout << "solution 함수의 반환 값은 " << (ret2 == true ? "true" : "false") << " 입니다." << endl;
}
반응형