string include string using namespace std void main

  • Slides: 31
Download presentation

string 클래스 사용하기 #include <string> using namespace std; void main() { string s; string

string 클래스 사용하기 #include <string> using namespace std; void main() { string s; string s = "Hello World!"; string s{ "Hello World!" }; } // string 객체 s를 생성한다. // string 객체를 생성하고 초기화한다.

문자열의 결합 #include <string> using namespace std; void main() { string subject = "Money";

문자열의 결합 #include <string> using namespace std; void main() { string subject = "Money"; string other = " has no value if it is not used"; string sentence = subject + other; }

예제 #include <iostream> #include <string> using namespace std; int main() { string s 1

예제 #include <iostream> #include <string> using namespace std; int main() { string s 1 = "Slow", s 2 = "steady"; string s 3 = "the race. "; string s 4; s 4 = s 1 + " and " + s 2 + " wins " + s 3; cout << s 4 << endl; return 0; }

문자열의 비교 #include <string> using namespace std; void main() { string s 1 =

문자열의 비교 #include <string> using namespace std; void main() { string s 1 = "Hello", s 2 = "World"; if( s 1 == s 2 ) cout << "동일한 문자열입니다. " << endl; else cout << "동일한 문자열이 아닙니다. " << endl; if( s 1 > s 2 ) cout << "s 1이 앞이 있습니다. " << endl; else cout << "s 2가 앞이 있습니다. " << endl; }

예제 #include <iostream> #include <string> using namespace std; int main() { string s 1,

예제 #include <iostream> #include <string> using namespace std; int main() { string s 1, addr; cout << "이름을 입력하시오 : "; cin >> s 1; cin. ignore(); // 엔터키를 없애기 위하여 필요하다. cout << "주소를 입력하시오 : "; getline(cin, addr); cout << addr << "의 " << s 1 << "씨 안녕하세요? " << endl; return 0; }

예제 #include <iostream> #include <string> using namespace std; int main() { string s="When in

예제 #include <iostream> #include <string> using namespace std; int main() { string s="When in Rome, do as the Romans. "; int index = s. find("Rome"); cout << index << endl; return 0; }

string 객체에서 문자 추출하기 #include <iostream> #include <string> using namespace std; int main() {

string 객체에서 문자 추출하기 #include <iostream> #include <string> using namespace std; int main() { string s; cout << "주민등록번호를 입력하시오: "; cin >> s; cout << "-가 제거된 주민등록번호: "; for (auto& c : s) { if (c == '-') continue; cout << c; } cout << endl; return 0; }

auto 키워드 자동 타입 추론(automatic type deduction) auto d = 1. 0; auto add(int

auto 키워드 자동 타입 추론(automatic type deduction) auto d = 1. 0; auto add(int x, int y) { return x + y; } int main() { auto sum = add(5, 6); 된다. return 0; }

문자열의 배열 #include <iostream> #include <string> using namespace std; int main() { string list[]

문자열의 배열 #include <iostream> #include <string> using namespace std; int main() { string list[] = { "철수", "영희", "길동" }; for (auto& x : list) cout << (x + "야 안녕!") << endl; return 0; }

#include <iostream> #include <string> using namespace std; string 객체에서 문자 추출하기 int main() {

#include <iostream> #include <string> using namespace std; string 객체에서 문자 추출하기 int main() { #include <iostream> string s 1, s 2; #include <string> int count = 0; using namespace std; } cout << "DNA 1: "; int main() cin >> s 1; { cout << "DNA 2: "; string s; cin >> s 2; cout << "주민등록번호를 입력하시오: "; cin >> s; if (s 1. length() != s 2. length()) cout << "오류: 길이가 다름" << endl; cout << "-가 제거된 주민등록번호: "; else { for (auto& c : s) { for (int i = 0; i < s 1. length(); i++) { if (c == '-') continue; if (s 1[i] != s 2[i]) cout << c; count += 1; } } cout << endl; cout << "해밍 거리는 " << count << endl; } return 0; }

solution #include <iostream> #include <string> using namespace std; int main() { char ch; string

solution #include <iostream> #include <string> using namespace std; int main() { char ch; string solution; string list[] = { "the", "c++", "programming", "language", };

int n = rand() % 4; solution = list[n]; string 객체에서 문자 추출하기 string

int n = rand() % 4; solution = list[n]; string 객체에서 문자 추출하기 string guess(solution. length(), '_'); // solution의 길이만큼 // ‘_’으로 초기화된 문자열 생성 while (true) { cout << guess << endl; cout << "글자를 입력하시오: "; cin >> ch; for (int i=0; i < solution. length(); i++) { if (ch == solution[i]) { guess[i] = ch; } } if (solution == guess) { cout << solution << endl; cout << "성공하였습니다. !"; break; } } return 0; }