ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 문자열 비교_220716
    데일리 codeup/문자열 2022. 7. 16. 15:45

    일치하는 문자열의 수

    정수 n과 문자열 A가 주어집니다. 그 다음 n개의 줄에 걸쳐 문자열들이 주어지면 문자열 A와 일치하는 문자열의 개수를 세는 프로그램을 작성해보세요.

    #include <iostream>
    #include <string>
    using namespace std;
    int main(){
        int n, cnt = 0;;
        string a,b;
        cin >> n >> a;
        for (int i=0;i<n;i++){
            cin >> b;
            if(a == b)  cnt++;
        }
        cout<<cnt;
        return 0;
    }

    문자열 거꾸로 출력하기

    알파벳으로 이루어진 문자열이 주어지면 거꾸로 뒤집어 출력하기를 반복하다가 'END'가 주어지면 작업을 종료하는 프로그램을 작성해보세요. 단, 'END'는 무조건 주어진다고 가정해도 좋습니다.

    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
    int main(){
        string s;
        while (true){
            cin >> s;
            if(s == "END")  break;
            reverse(s.begin(),s.end());
            cout << s << endl;
        }
        return 0;
    }

    algorithm헤더 안에 있는 reverse(pos_begin, pos_end)함수는 pos_begin부터 pos_end를 역순으로 나열한 문자열을 return한다


    문자열의 개수

    알파벳으로 이루어진 문자열이 계속 주어지다가 '0'이 주어지면 입력을 종료하고 숫자 0이 주어지기 전까지의 문자열의 개수를 출력하고 홀수 번째 주어진 문자열을 한 줄에 1개씩 출력하는 프로그램을 작성해보세요. 단, '0'은 무조건 주어진다고 가정해도 좋습니다.

    #include <iostream>
    #include <string>
    using namespace std;
    int main(){
        string s[200];
        int cnt = 0;
        while (true){
            cin >> s[cnt];
            if(s[cnt]=="0") break;
            cnt++;
        }
        cout<<cnt<<endl;
        for(int i=0;i<cnt;i+=2){
            cout<<s[i]<<endl;
        }
        return 0;
    }

    미는 횟수

    문자열 A를 우측으로 한 칸씩 n번 밀었을 때, 문자열 A 와 문자열 B가 같아지는 순간의 n 중 가능한 최솟값을 구하는 프로그램을 작성해보세요. (n > 0)

    #include <iostream>
    #include <string>
    using namespace std;
    int main(){
        string a,b;
        cin >> a >> b;
        int n = 0;
        while(a != b){
            a = a[a.length()-1] + a.substr(0,a.length()-1);
            n ++;
            if(n>a.length()){
                cout<< -1;
                return 0;
            }
        }
        cout<<n;
        return 0;
    }

    댓글

Designed by Tistory.