2019/c++

Strings(2)

fw93 2018. 3. 22. 16:31
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
 
using namespace std;
 
void touppercase(char * sam) {
    while (*sam) {
        if (*sam >= 'a' && *sam <= 'z') {
            *sam = *sam - 'a' + 'A';
        }
        sam++;
    }
    //sam 포인터는 원본 포인터 주소값을 변경하지는 않는다. *sam은 원본 값을 변경하기는한다.
}
 
void reverse(char * sam) {
    char * end = sam;
    while (*end) {
        end++;
    }
    end--;
    while (end > sam) {
        char tmp = *end;
        *end = *sam;
        *sam = tmp;
        sam++;
        end--;
    }
}
 
bool duplicate(string s) {
    vector<char> my(s.begin(), s.end());
    sort(my.begin(),my.end());
    for (int i = 0; i < my.size(); i++) {
        if (i != my.size() - 1 && my.at(i) == my.at(i + 1)) {
            return true;
        }
    }
    return false;
}
 
string addhello(string s) {
    cout << *(s.end() - 1<< endl;
    vector<char> my(s.begin(), s.end()); // NULL은 생략한다.
    vector<char>::iterator it;
    for (it = my.begin(); it != my.end(); it++) {
        if (*it == '\0') {
            printf("null found\n");
        }
    }
    my.push_back('h');
    my.push_back('e');
    my.push_back('l');
    my.push_back('l');
    my.push_back('o');
    my.push_back('\0');
    string s1(my.begin(), my.end());
    return s1;
}
 
int main() {
string sss = "hello world;
char * tmp = &sss[0];
printf("%s",tmp);
    string s2 = "junho";
    s2 = addhello(s2);
    printf("%s", s2.c_str());
    while (1) {}
    char sam[50= "hello world";
    char * junho = sam;
    string s = "helo junho";
    if (duplicate(s)) {
        printf("duplicated\n");
    }
    sam[3= 'z';
    const char * yy = "alphabet";
    //yy[3] = 'z'; const 선언 때문에 불가능
    printf("%s\n", junho);
    printf("%s\n", yy);
    touppercase(junho);
    reverse(junho);
    printf("%s\n", junho);
    while (1) {}
    return 0;
}
scs


'2019 > c++' 카테고리의 다른 글

string manipulations ( pointers )  (0) 2018.03.22
strings(3)  (0) 2018.03.22
relational data structures & strings (C++ STL)  (0) 2018.03.21
dynamic allocation of 2-dimension array  (0) 2018.03.20
c++ reference variables  (0) 2018.03.18