์์คํค์ฝ๋(ASCII) : ๋ฌธ์ ์ธ์ฝ๋ฉ ๋ฐฉ๋ฒ ์ค ํ๋์ด๋ค.
์ค์ต
์ํ๋ฒณ ๊ฐ์
Copy #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ํด์ค๋ค.
์ํ๋ฒณ ์์น
Copy #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๊ฐ์ ํฌํจ๋์ด ์๋ ์๋ฌธ์, ๋๋ฌธ์, ์ซ์, ๊ณต๋ฐฑ์ ๊ฐ์๋ฅผ ์ธ๋ ๋ฌธ์
Copy #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' ;
}
๋จ์ด ๊ธธ์ด ์ฌ๊ธฐ
Copy scanf ( " %s " , s);
int len = 0 ;
for ( int i = 0 ; s [i];i ++ ){ //s[i]์ NULL๊ฐ์ด ๋์ค๋ฉด ์ข
๋ฃ๋๋ค.
len += 1 ;
}
ROT13
ROT13์ผ๋ก ์ํธํํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋๋ ๋ฌธ์ ์ด๋ค. ROT13์ ์นด์ด์ฌ๋ฅด ์ํธ์ ์ผ์ข
์ผ๋ก ์์ด ์ํ๋ฒณ์ 13๊ธ์์ฉ ๋ฐ์ด์ ๋ง๋ ๋ค.
Copy #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
๋ฑ๋ฑ์ ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
string->unsigned long long
์ ์ -> ๋ฌธ์์ด
to_string
ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด๋๋ค.
Copy #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์ ๋ชจ๋ ์ ๋ฏธ์ฌ๋ฅผ ์ฌ์ ์์ผ๋ก ์ ํด ๋์ ๋ฐฐ์ด์ด๋ค.
Copy ex)likelion
likelion, ikelion, kelion, elion, lion, ion, on, n
Copy #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" ;
}
}