C++ Programming Ch.4 Exercise 5 Solution
Problem: string ν΄λμ€λ₯Ό μ΄μ©νμ¬ μ¬μ©μκ° μ λ ₯ν μλ¬Έ ν μ€μ μ λ ₯λ°κ³ κΈμ νλλ§ λλ€νκ² μμ νμ¬ μΆλ ₯νλ νλ‘κ·Έλ¨μ μμ±νλΌ. Objective & Hints: string ν΄λμ€λ‘ λ¬Έμμ΄ λ€λ£¨κΈ° λλ€ μ μλ₯Ό λ°μμν€κΈ° μν΄ λ€μ λ λΌμΈμ μ½λκ° νμνλ©°, μ ν€λ νμΌμ include ν΄μΌ νλ€. 1 2 srand((unsinged)time(0)); // μμν λλ§λ€, λ€λ₯Έ λλ€μλ₯Ό λ°μμν€κΈ° μν seed μ€μ int n = rand(); // 0μμ RAND_MAX(32767) μ¬μ΄μ λλ€ν μ μ λ°μ Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include #include #include using namespace std; int main() { string sent; int n; cout << "μλ ν μ€μ μ λ ₯νμΈμ.(exitλ₯Ό μ λ ₯νλ©΄ μ’ λ£ν©λλ€)" ; while(true){ srand((unsigned)time(0)); cout << "\n>>"; getline(cin,sent); if(sent == "exit") break; int length = sent.length(); while(true){ n = rand()%length; if(sent[n]!=' ') break; } int a = rand()%25+95; // μμμ λ¬Έμ νλ μ ν sent[n] = (char)a; cout << sent; } } Explanation: ...