ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 문자 입력_220701
    데일리 codeup/입출력 2022. 7. 1. 15:31

    c++에서 문자 1개 입력은 char 변수를 이용하여 받을 수 있다.

    예제

    #include <iostream>
    using namespace std;
    
    int main() {
    
        char a;
        cin >> a;
        cout << a;
    
        return 0;
    
    }

    출력결과

    >> b
    
    b

    문자 받아 출력

    문자 c를 하나 입력받아 그대로 출력하는 프로그램을 작성해주세요.

    #include <iostream>
    using namespace std;
    int main(){
        char a;
        cin >> a;
        cout << a;
        return 0;
    }

    문자열 입력

    c에서 문자열 1개 입력은 string type 을 이용해 받을 수 있다.

    string 배열을 이용하기 위해서는 #include <string> 헤더를 포함시켜주어야 한다.

    예제

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
    
        string a;
        cin >> a;
        cout << a;
    
        return 0;
    
    }

    출력결과

    >> cat
    
    cat

    문자열 받아 출력

    문자열 s를 하나 입력받아 그대로 출력하는 프로그램을 작성해주세요.

    #include <iostream>
    #include <string>
    using namespace std;
    int main(){
        string s;
        cin >> s;
        cout << s;
        return 0;
    }

    실수와 문자 받아 출력하기

    문자c와 실수 a,b를 입력받아 출력하되 실수는 반올림하여 소수 둘째자리까지 출력하는 프로그램을 작성해주세요.

    #include <iostream>
    #include <string>
    using namespace std;
    int main(){
        string s;
        double a, b;
        cin >> s >> a >> b;
        cout << s << endl;
        cout << fixed;
        cout.precision(2);
        cout << a << endl << b;
        return 0;
    }

    입력

    C
    89.84336
    234.5678

    출력 결과

    C
    89.84
    234.57

    문자열 순서 바꾸기

    문자열 s와 문자열 t를 입력받아 순서를 바꾸어 출력하는 프로그램을 작성해보세요.

    #include <iostream>
    #include <string>
    using namespace std;
    int main(){
        string s,t;
        cin >> s >> t;
        cout << t << endl << s;
        return 0;
    }

    입력

    code
    tree

    출력 결과

    tree
    code

    댓글

Designed by Tistory.