String

์•„์Šคํ‚ค์ฝ”๋“œ(ASCII) : ๋ฌธ์ž ์ธ์ฝ”๋”ฉ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ด๋‹ค.

  • 0 => 48

  • A => 65

  • a => 97

  • 0 => NULL

์‹ค์Šต

์•ŒํŒŒ๋ฒณ ๊ฐœ์ˆ˜

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    string s;
    cin >> s;

    for (int i='a'; i<='z'; i++) {
        cout << count(s.begin(), s.end(), i) << ' ';
    }

    cout << '\n';
}

์—ฌ๊ธฐ์„œ #include <algorithm>์—์„œ count(s.begin(),s.end(),i);๋Š” s๋ฌธ์ž์—ด์—์„œ i๊ฐ€ ํฌํ•จ๋œ ์ˆ˜๋ฅผ countํ•ด์ค€๋‹ค.

์•ŒํŒŒ๋ฒณ ์œ„์น˜

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;


void alpha_loc() {
    string s;
    cin >> s;

    for (int i='a'; i<='z'; i++) {
        auto it = find(s.begin(), s.end(), i);
        if (it == s.end()) {
            cout << -1 << ' ';
        } else {
            cout << (it - s.begin()) << ' ';
        }
    }
    cout << '\n';
}

find(s.begin(), s.end(), i);๋Š” ์œ„์น˜๋ฅผ ์•Œ๋ ค์ค€๋‹ค.

๋ฌธ์ž์—ด ๋ถ„์„

๋ฌธ์ž์—ด N๊ฐœ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ์†Œ๋ฌธ์ž, ๋Œ€๋ฌธ์ž, ์ˆซ์ž, ๊ณต๋ฐฑ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๋ฌธ์ œ

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    string s;
    while(getline(cin,s)){
        int upper=0,lower=0,space=0,number=0;
        for(int i=0;i<s.length();i++){
            if('a'<=s[i]&&s[i]<='z'){
                lower+=1;
            }else if('A'<=s[i]&&s[i]<='Z'){
                upper+=1;
            }else if('0'<=s[i]&&s[i]<='9'){
                number+=1;
            }else if(s[i]==' '){
                space+=1;
            }
        }
        cout << lower <<' '<< upper <<' '<< number <<' '<< space<<'\n';
    }

    cout << '\n';
}

๋‹จ์–ด ๊ธธ์ด ์žฌ๊ธฐ

  • strlen

  • s.length()

  • s.size()

  • scanf("%s",s);
    int len = 0;
    for(int i=0;s[i];i++){//s[i]์— NULL๊ฐ’์ด ๋‚˜์˜ค๋ฉด ์ข…๋ฃŒ๋œ๋‹ค.
      len+=1;
    }

ROT13

ROT13์œผ๋กœ ์•”ํ˜ธํ™”ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ๋Š” ๋ฌธ์ œ์ด๋‹ค. ROT13์€ ์นด์ด์‚ฌ๋ฅด ์•”ํ˜ธ์˜ ์ผ์ข…์œผ๋กœ ์˜์–ด ์•ŒํŒŒ๋ฒณ์„ 13๊ธ€์ž์”ฉ ๋ฐ€์–ด์„œ ๋งŒ๋“ ๋‹ค.

#include <iostream>
#include <string>

using namespace std;

int main() {
    string s;
    getline(cin,s);
    int n = s.size();
    for(int i=0;i<n;i++){
        if('a'<=s[i]&&s[i]<='m'){
            s[i]=s[i]+13;
        }else if('n'<=s[i]&&s[i]<='z'){
            s[i]=s[i]-13;
        }else if('A'<=s[i]&&s[i]<='M'){
            s[i]=s[i]+13;
        }else if('N'<=s[i]&&s[i]<='Z'){
            s[i]=s[i]-13;
        }
    }
    cout << s <<"\n";
}

๋ฌธ์ž์—ด -> ์ •์ˆ˜

#include <string>์—์„œ stoi,stol,stoll๋“ฑ๋“ฑ์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

ํ•จ์ˆ˜

๋ณ€ํ™”

stoi

string->int

stol

string->long

stoll

string->long long

stof

string->float

stod

string->double

stold

string->long double

stoul

string->unsigned long

stoull

string->unsigned long long

์ •์ˆ˜ -> ๋ฌธ์ž์—ด

to_stringํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด๋œ๋‹ค.

#include <iostream>
#include <string>

using namespace std;

int main() {
    long a,b,c,d;
    cin >> a >> b >> c >> d;

    string ab = to_string(a)+ to_string(b);
    string cd = to_string(c)+ to_string(d);

    cout << stoll(ab)+stoll(cd)<<"\n";
}

์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด

๋ฌธ์ž์—ด S์˜ ๋ชจ๋“  ์ ‘๋ฏธ์‚ฌ๋ฅผ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ •ํ•ด ๋†“์€ ๋ฐฐ์—ด์ด๋‹ค.

ex)likelion
likelion, ikelion, kelion, elion, lion, ion, on, n
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    string s,str;
    cin >> s;

    vector<string> v;

    for(int i=0;i<s.size();i++){
        v.push_back(str.assign(s,i,s.size()));
    }
    sort(v.begin(),v.end());
    for(int i=0;i<v.size();i++){
        cout << v[i]<<"\n";
    }
}

Last updated