๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[๊ฐ์ฒด ํฌ์ธํ„ฐ์™€ ๊ฐ์ฒด ๋ฐฐ์—ด, ๊ฐ์ฒด์˜ ๋™์  ์ƒ์„ฑ] ๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป IoT (Embedded)/C++

[๊ฐ์ฒด ํฌ์ธํ„ฐ์™€ ๊ฐ์ฒด ๋ฐฐ์—ด, ๊ฐ์ฒด์˜ ๋™์  ์ƒ์„ฑ] ๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ

์ง•์ง•์•ŒํŒŒ์นด 2024. 2. 22. 01:18
728x90
๋ฐ˜์‘ํ˜•

<ํ™ฉ๊ธฐํƒœ ๋‹˜์˜ ๋ช…ํ’ˆ C++ Programming ์„œ์ ์„ ์ฐธ๊ณ ํ•ด์„œ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค :-)>

 

๐Ÿ ๋ฌธ์ž์—ด ์น˜ํ™˜

string a = "hi";
string b = "hello";

a = b;

 

๐Ÿ ๋ฌธ์ž์—ด ๋น„๊ต compare

๋‘ ๋ฌธ์ž์—ด์ด ๊ฐ™์œผ๋ฉด 0, str ๋ณด๋‹ค ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์•ž์— ์˜ค๋ฉด ์Œ์ˆ˜, ๋’ค์— ์˜ค๋ฉด ์–‘์ˆ˜

string name = "gani";
string alias = "tani";

int res = name.compare(alias);

if (res == 0) cout << "๋ฌธ์ž์—ด ๊ฐ™์Œ" << endl;
else if (res < 0) cout << name << " < " << alis << endl;
else cout << alis << " < " << name << endl;

 

๐Ÿ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ append

string a("I love you");
string b(".");
string c;

c = a + b;
c += b;

 

๐Ÿ ๋ฌธ์ž์—ด ์‚ฝ์ž… insert, replace

string a("I love you");
a.insert(2, "really");

a.replace(2, 11, "study");

 

๐Ÿ ๋ฌธ์ž์—ด ๊ธธ์ด length, size, capacity

๋ฌธ์ž์—ด์— ํฌํ•จ๋œ ๋ฌธ์ž ๊ฐœ์ˆ˜

length() ์™€ size() ๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค

capacity() ๋Š” ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ๋ฉ”๋ชจ๋ฆฌ ์šฉ๋Ÿ‰์„ ๋ฆฌํ„ดํ•œ๋‹ค

string a("I love you");
int length = a.length();
int size - a.size();
int capacity = a.capacity();

 

๐Ÿ ๋ฌธ์ž์—ด ์‚ญ์ œ erase, clear

arase() ๋Š” ๋ฌธ์ž์—ด ์ผ๋ถ€๋ถ„ ์‚ญ์ œ

clear() ์€ ์™„์ „ํžˆ ์‚ญ์ œ

string b = "I love C++");
a.eras(0, 7);
a.clear();

 

๐Ÿ ์„œ๋ธŒ์ŠคํŠธ๋ง

๋ฌธ์ž์—ด์—์„œ ์ผ๋ถ€๋ถ„์„ ๋ฐœ์ทŒํ•œ ๋ฌธ์ž์—ด (์„œ๋ธŒ์ŠคํŠธ๋ง)์„ ์–ป๋Š”๋‹ค

substr() ์€ ๋ฌธ์ž์—ด ๋ณ€ํ™”๊ฐ€ ์—†์Œ

string b = "I love C++");
string c = b.substr(2, 4);
string d = b.usbstr(2);

 

๐Ÿ ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰

string a = "I love C++");

int index = a.find("love");
index = a.find("love", index 3);

 

๐Ÿ ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž ๋‹ค๋ฃจ๊ธฐ

at() ํ•จ์ˆ˜์™€ [] ์—ฐ์‚ฐ์ž๋Š” ๋ฌธ์ž์—ด์˜ ํŠน์ • ์œ„์น˜์— ์žˆ๋Š” ๋ฌธ์ž๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค

string a = "I love C++");
char ch1 = a.at(7);
char ch2 = a[7];

a[7] = "D";

 

๐Ÿ ๋ฌธ์ž์—ด์˜ ์ˆซ์ž ๋ณ€ํ™˜, stoi

string year = "2024";
int n = stoi(year);

 

๐Ÿ ๋ฌธ์ž ๋‹ค๋ฃจ๊ธฐ

<locale> ์— ์žˆ๋Š” touuper, isdigit, isalpah ํ•จ์ˆ˜๋กœ ๋ฌธ์ž ๋‹ค๋ฃจ๊ธฐ

string a = "hello";
for (int i = 0; i < a.length(); i++) { a[i] = toupper(a[i]); }
cout << a;

if (isdigit(a[0])) cout << "์ˆซ์ž";
else if (isalpha(a.at(0))) cout << "๋ฌธ์ž";

 

๐Ÿ ๋ฌธ์ž์—ด ์‘์šฉ ๋ฌธ์ œ

// ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›๊ณ  ํšŒ์ „์‹œํ‚ค๊ธฐ
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s;

    cout << "์•„๋ž˜์— ๋ฌธ์ž์—ด ์ž…๋ ฅํ•˜์„ธ์š”." << endl;
    getline(cin, s, '\n');

    for (int i = 0; i < s.length(); i++)
    {
        string first = s.substr(0, 1);
        string sub = s.substr(1, s.length() - 1);

        s = sub + first;
        cout << s << endl;
    }
}

/*
I love you
 love youI
love youI
ove youI l
ve youI lo
e youI lov
 youI love
youI love
ouI love y
uI love yo
I love you
*/
// ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ์‘์šฉ - ๋ง์…ˆ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ๋ง์…ˆ ์‹คํ–‰
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s;
    cout << "๋ง์…ˆ ๋ฌธ์ž์—ด ์ž…๋ ฅํ•˜๊ธฐ" << endl;
    getline(cin, s, '\n');

    int sum = 0;
    // ๋ฌธ์ž์—ด ๋‚ด์— ๊ฒ€์ƒ‰ํ•  ์‹œ์ž‘ ์ธ๋ฑ์Šค
    int startIndex = 0;

    while (true)
    {
        // "+" ๋ฌธ์ž ๊ฒ€์ƒ‰
        int fIndex = s.find('+', startIndex);
        // "+" ๋ฌธ์ž ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์—†์Œ
        if (fIndex == -1)
        {
            string part = s.substr(startIndex);
            // "+" ์œผ๋กœ ๋๋‚˜๋Š” ๊ฒฝ์šฐ
            if (part == "")
                break;
            cout << part << endl;
            // ๋ฌธ์ž์—ด์„ ์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋”ํ•˜๊ธฐ
            sum += stoi(part);
            break;
        }
        // ์„œ๋ธŒ์ŠคํŠธ๋ง์œผ๋กœ ์ž๋ฅผ ๋ฌธ์ž ๊ฐœ์ˆ˜
        int count = fIndex - startIndex;
        string part = s.substr(startIndex, count);

        cout << part << endl;
        // ๋ฌธ์ž์—ด์„ ์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋”ํ•˜๊ธฐ
        sum += stoi(part);
        // ๊ฒ€์ƒ‰์„ ์‹œ์ž‘ํ•  ์ธ๋ฑ์Šค ์ „์ง„์‹œํ‚ด
        startIndex = fIndex + 1;
    }

    cout << "์ˆซ์ž๋“ค์˜ ํ•ฉ์€ " << sum;
}

/*
๋ง์…ˆ ๋ฌธ์ž์—ด ์ž…๋ ฅํ•˜๊ธฐ
66+4+24+3+25
66
4
24
3
25
์ˆซ์ž๋“ค์˜ ํ•ฉ์€ 122
*/
// ๋ฌธ์ž์—ด find ๋ฐ replace
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s;
    cout << "์—ฌ๋Ÿฌ ์ค„์˜ ๋ฌธ์ž์—ด ์ž…๋ ฅํ•˜์„ธ์š”. ์ž…๋ ฅ์˜ ๋์€ & ๋ฌธ์ž" << endl;
    getline(cin, s, '&');
    // enter ํ‚ค ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ
    cin.ignore();

    string f, r;
    cout << endl
         << "find : ";
    getline(cin, f, '\n'); // ๊ฒ€์ƒ‰ํ•  ๋ฌธ์ž์—ด ์ž…๋ ฅ
    cout << "replace : ";
    getline(cin, r, '\n'); // ๋Œ€์น˜ํ•  ๋ฌธ์ž์—ด ์ž…๋ ฅ

    int startIndex = 0;
    while (true)
    {
        int fIndex = s.find(f, startIndex);
        if (fIndex == -1)
        {
            break;
        }

        // fIndex ๋ถ€ํ„ฐ ๋ฌธ์ž์—ด f์˜ ๊ธธ์ด๋งŒํผ ๋ฌธ์ž์—ด r๋กœ ๋ณ€๊ฒฝ
        s.replace(fIndex, f.length(), r);
        startIndex = fIndex + r.length();
    }

    cout << s << endl;
}

/*
์—ฌ๋Ÿฌ ์ค„์˜ ๋ฌธ์ž์—ด ์ž…๋ ฅํ•˜์„ธ์š”. ์ž…๋ ฅ์˜ ๋์€ & ๋ฌธ์ž
hi, today is thursday! I am very hungry. And I will sleep now. say hello! hi girl~ we need to go to bed right now~ see sddsson &

find : now
replace : nooooooooow
hi, today is thursday! I am very hungry. And I will sleep nooooooooow. say hello! hi girl~ we need to go to bed right nooooooooow~ sddsson*/
728x90
๋ฐ˜์‘ํ˜•
Comments