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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
 
int main() {
    string line;
    cout << "아래에 한 줄을 입력하세요.(exit를 입력하면 종료합니다)" << endl;
    while (1) {
        cout << ">>";
        getline(cin, line);
        if (line == "exit") {
            break;
        }
        srand((unsigned)time(0));
        int size = line.length();
        int r = rand() % size;
        int random = rand();        // char c 대소문자 구분을 위한 난수
        char c;
        while (1) {
            if (isalpha(line[r])) {        // 선택한 문자가 영문자인지 확인
                if (random % 2 == 0) {    // 난수가 짝수면
                    c = (char)(rand() % 26 + 65);    // c 대문자
                    while (1) {
                        if (line[r] == c) {        // 랜덤하게 선택한 문자가 랜덤문자와 같다면
                            c = (char)(rand() % 26 + 65);    // 다시 랜덤문자 생성
                        }
                        else {                    // 랜덤하게 선택한 문자가 랜덤문자와 다르다면
                            break;                            // break
                        }
                    }
                }
                else {    // 난수가 홀수면
                    c = (char)(rand() % 26 + 97);    // c 소문자
                    while (1) {
                        if (line[r] == c) {        // 랜덤하게 선택한 문자가 랜덤문자와 같다면
                            c = (char)(rand() % 26 + 97);    // 다시 랜덤문자 생성
                        }
                        else {                // 랜덤하게 선택한 문자가 랜덤문자와 다르다면
                            break;                            // break
                        }
                    }
                }
                line[r] = c;    // 최종 c값 대입
                break;
            }
            else {                    // 선택문자가 영문자가 아니면
                r = rand() % size;    // 영문자 다시 찾으러가자~
                continue;
            }
        }
        cout << line << endl;
    }
}
cs

+ Recent posts