# 字串函數 & 函數呼叫

Arvin Liu @ Sprout

tlk.io/clang

10.5.6.94:8788

## 點名網址

xx.x.x.xx:8788/recorder

# #include <函式庫名稱>

## Step 1 - 知道別人在哪裡 / 告訴編譯器你要用哪裡的函數

#include <iostream>就是一種。

# String review

## 偷渡指標的概念...

``````char A[] = "123456789";
std::cout << A << std::endl;
std::cout << A+3 << std::endl;
std::cout << A+6 << std::endl;
``````
``````Output:
123456789
456789
789``````

## 偷渡指標的概念...

``````char A[] = "123456789";
std::cout << A << std::endl;
std::cout << A+3 << std::endl;
std::cout << A+6 << std::endl;
``````
1 2 3 4 5 6 7 8 9 '\0'

A

A+3

A+6

# Example

``````char S[100];
cin >> S;
cout << strlen(S);``````
``````Input:
abcdef``````
``````Output:
6``````

# Warning!

``````for(int i=0; i< strlen(S); i++)
// do something``````
``````int len = strlen(S);
for(int i=0; i< len ; i++)
// do something``````

Maybe TLE (它其實是個雙層迴圈)

Maybe AC

# Example

``````char S[100];
cin >> S;
cout << (strcmp(S,"QAQ") == 0);``````
``````Input:
abcdef
-------
QAQ``````
``````Output:
0
----
1``````

# Warning!

``````if("ABCD" == S){
// Do something.
}``````

# Example

``````char str[80];
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated.");
puts (str);
``````
``````Output:
these strings are concatenated.``````

# Example

``````char str1[] = "Sample string";
char str2[40];
strcpy (str2,str1);
puts (str2);
``````
``````Output:
Sample string``````

## Review

• strcmp - string compare
• strcat - string concatenate
• strcpy - string copy
• strlen - string length

# 剛剛說的函數 做做看喔！

## Solution Explanation

`0123456789`
`9012345678`
`8901234567`
`7890123456`
`6789012345`
`5678901234`
`4567890123`
`3456789012`
`2345678901`
`1234567890`

S+len-1

S+len-2

S+len-3

S+len-4

## Solution Explanation

`01234567890`
`901234567890`
`8901234567890`
`78901234567890`
`678901234567890`
`5678901234567890`
`45678901234567890`
`345678901234567890`
```2345678901234567890
12345678901234567890
```

strcat

## Solution Explanation

'\0'

`01234567890`
`901234567890`
`8901234567890`
`78901234567890`
`678901234567890`
`5678901234567890`
`45678901234567890`
`345678901234567890`
```2345678901234567890
12345678901234567890
```

``````#include <iostream>
#include <cstring>
using namespace std;
int main(){
char S[1001],S2[2001];
cin >> S;
int len = strlen(S);
for( int i = len-1 ; i>=0 ; i-- ){
strcpy(S2,S+i);
strcat(S2,S);
S2[len] = 0;
cout << S2 << endl;
}
}``````

# 嘗試用你的code 解出上週的marquee吧！

Hint: 外面一層while迴圈，每次輸出都Sleep，最後在換行的地方改成回車符號。

# Homework Solution

## 有很多字 (20)

char Spell_book [100][100][161];

## 第j個字節的 第k個char

Spell_book [i][j][k]

## 的第k個字節一不一樣？

strcmp( Spell_book [i][k],
Spell_book [j][k] ) 是不是0

## Solution Code

``````#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
char S[100][100][161];
int main(){
int N;
cin >> N;
for(int i=0;i<N;i++){
int M;
cin >> M;
for(int j=0;j<M;j++){
cin >> S[i][j];
}
}
for(int i=0;i<N;i++){
int max_match = 0;
for(int j=0;j<N;j++){
if(i == j)
continue;
else{
for(int k=0;k<100;k++){
if(strcmp(S[i][k],S[j][k])!=0){
max_match = max(max_match,k);
break;
}
}
}
}
cout << max_match +1 << endl;
}
return 0;
}``````

By Arvin Liu

# 字串函數 & 函數呼叫

Teaching slide - Function call & string.h

• 654