---
# System prepended metadata

title: String

---

---
type: slide
---

# String

---

## How to save an sentance

----

char+char+

----

char+char+char+char+char+char+char+char+

----

char+char+char+char+char+char+char+char+
char+char+char+char+char+char+char+char+
char+char+char+char+char+char+char+char+
char+char+char+char+char+char+char+char+

----

```cpp
char str[] = 'abc';
str = {'a','b','c'};
```

---

- std::string
- 好用的char[]

----

```cpp
string str;
cin >> str;
cout << str << '\n';
```

----

```cpp
string str = "abcde", str1 = "fg";
    
//str[0] = 'a';
//str[2] = 'c';
    
string str_new = str + str1;
//str_new = "abcdefg"

//str_new.size() = 7
```

----

```cpp
"aaa" == "aaa" // 相同
"aaa" != "bbb" // 不相同
"abcd" < "abcde" // 規則 前面的字相同 比長度
"abcd" > "abcc" // 比字典序
"abcd" > "abcceeeeee" // d > c 即使右邊比較長
```

---

- csdc 292

----

從頭到尾與從尾到頭一樣的string

----

```cpp
#include<bits/stdc++.h>
using namespace std;

int main(){
	string S;
	cin >> S;
	for(int i = 0; i < S.size()/2;i++){
		if(S[i] != S[S.size()-i-1]){
			cout << "0";
			return 0;
		}
	}
	cout << "1";
	return 0;
}
```

---

- csdc 79

----


先初始化數字為0。然後從第一個字元開始，依序判斷每一位字元要做的事。
如果為'w'、'y'、'b'，請將數字加一，
如果為'g'、'r'，則將數字減一，
如果為'o'，請輸出目前數字。

----

輸入
w、y、b、g、r、o各數個共24個字元所組成的字串，如："wwwwyyyybbbbggggrrrroooo"

輸出
照著題目規則輸出

----

```cpp
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str; cin >> str;
    
    int total = 0;
    
    for(int i = 0; i < str.size(); i++ )
    {
        if(str[i] =='w' || str[i] == 'y' || str[i] == 'b')
        {
            total++;
        }
        else if(str[i] == 'g' || str[i] == 'r')
        {
            total--;
        }
        else cout << total;
    }
    
    cout << '\n';
    
    return 0;
}

```

---

# ascii

----

![](https://hackmd.io/_uploads/Bk-KTJLMT.png)


---

# 凱薩密碼
zero judge b428

----



----


```cpp
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str;
    
    while(cin >> str)
    {
        string str_2; cin >> str_2;
        
        int num = (str_2[0] - str[0]) % 26;
        
        if(num<0) num+=26;
        
        cout << num << '\n';
    }
    
    return 0;
}

```