인프런 커뮤니티 질문&답변
1-f 코드 질문드립니다!
해결된 질문
작성
·
123
·
수정됨
0
http://boj.kr/173bd797559a4a5d88f48a6dee8c0d05
요렇게 풀었는데 런타임 에러가 나서 이유가 궁금해서 질문드립니다!
cout으로 디버깅해봤는데
위 코드에서 inputWord 는 길이가 출력되지만 rot13Word 는 0 으로 출력됩니다!
답변 1
1
큰돌
지식공유자
안녕하세요 인준님 ㅎㅎ
            if (inputWord[i] + 13 > 90)
            {
                rot13Word[i] = inputWord[i] + 13 - 26;이부분에서 에러가 뜨는 것 같습니다. 크기를 지정하지 않은 string에서 i번째로 계속해서 참조를 하고 있는데요. 이렇게 하시면 안되고 += 식으로 하시면 됩니다.
나머지 로직 부분은 정말 완벽합니다. 잘 짜셨습니다.
제가 다듬은 코드는 다음과 같습니다.
#include <iostream>
#include <string>
using namespace std;
string inputWord, rot13Word;
int main()
{
    getline(cin, inputWord);
    if (inputWord.length() > 100)
    {
        return 0;
    }
    for (int i = 0; i < inputWord.length(); i++)
    {
        if (65 <= inputWord[i] && inputWord[i] <= 90) 
        {
            if (inputWord[i] + 13 > 90)
            {
                rot13Word += inputWord[i] + 13 - 26;
            }
            else
            {
                rot13Word += inputWord[i] + 13;
            }
        }
        else if (97 <= inputWord[i] && inputWord[i] <= 122) 
        {
            if (inputWord[i] + 13 > 122)
            {
                rot13Word += inputWord[i] + 13 - 26;
            }
            else
            {
                rot13Word += inputWord[i] + 13;
            }
        }
        else
        {
            rot13Word += inputWord[i];  
        }
    }
    cout << rot13Word << "\n"; 
    return 0;
}
 
또 질문 있으시면 언제든지 질문 부탁드립니다.
좋은 수강평과 별점 5점은 제게 큰 힘이 됩니다. :)
감사합니다.
강사 큰돌 올림.





