字串

string 

-葉子齊-

Index

  • 單一字元
  • 可轉換成ASCII碼
  • 一個或以上的字元組成

字元 char

ASCII表

  • 一種將字元對應到數值的標準編碼方式
  • 開發始於 1963,它的創建是為了改進電報中使用的程式碼
  • "美國資訊交換標準代碼"的縮寫

ASCII表

只需要知道:

  • a比z的ASCII小
  • A比Z的ASCII小
  • 0比9的ASCII小

詳細版:

  • 數字字元 '0' ~ '9':ASCII 值為 48 ~ 57

  • 大寫字母 'A' ~ 'Z':ASCII 值為 65 ~ 90

  • 小寫字母 'a' ~ 'z':ASCII 值為 97 ~ 122

 顯示 ASCII 碼

可利用 (int) 將字元強制轉型為整數,取得對應的 ASCII 碼:

#include <iostream>
using namespace std;
int main()
{
    char ch = 'a';
    cout << "ASCII 碼為:" << (int)ch << endl;


    return 0;
}

字元 & 字串

正式進入主題辣

字元 

在 C++ 中,字元使用 char 資料型別表示,

一個 char 變數可以儲存一個 ASCII 字元

char ch = 'A'; 

使用單引號' '表示字元

判斷字元類型

輸入一個字元,

若為大寫輸出upper,

小寫輸出lower,

不是字母則輸出Not a letter

char ch;
cin >> ch;
if(ch>='A' && ch<='Z') cout << "upper" << endl;
else if(ch>='a' && ch<='z') cout << "lower" << endl;
else cout << "Not a letter" << endl;

判斷字元類型

#include <iostream>
using namespace std;
int main()
{
    char ch; 
    while(cin>>ch){
        
        if (isupper(ch)) cout << "是大寫字母";
        else if (islower(ch))  cout << "是小寫字母";
        else if (isdigit(ch)) cout << "是數字";
        else cout << "是其他符號";
        
    }

    return 0;
}

判斷字元類型

更多判斷字元類型的功能可以函式庫<cctype>

參考資料:

字串 string

字串

字串是多個字元組成的,使用 string 資料型態

#include <string>

#include <string>
string name = "Leaf";

使用雙引號" "

字串

  • 輸入時遇到空白或換行停止
string str1, str2;
cin >> str1 >> str2;

輸入 :  Hello World

輸入 :  Hello

             World

 

結果 :  str1 = Hello

             str2 = World

常用用法

  • 可取出其中的單一字元
  • 宣告後可更改其中的內容(部分or全部)
  • 相等比較 == : 比較兩個字串的字元內容是否相同
  • 串接字串 + : 直接使用 + 運算子來串接字串
  • 字串長度 str.size() : 字串長度
  • 字串長度 str.length() : 字串長度
  • 字串為空 str.empty() : 字串是否為空

 

注意事項

  • #include<string>
  • 使用雙引號(")
  • 輸入時遇到空白或換行停止

常用用法

  • 可取出其中的單一字元
  • 可相加
string str = "Hello World!";
char first = str[0];
char second = str[1];
cout << first <<" "<< second;
//H e
string first = "Hello ";
string second = "World!";
cout << first + second;
//Hello World!

常用用法

  • 宣告後可更改其中的內容(部分or全部)
string str = "Hello World!";
str[1]='a';
cout << str;
//Hallo World!
string str = "Hello World";
str = "new Hello World";
cout << str << endl;
//new Hello World

常用用法

string str1; 
string str2 = "Hello"; 
string str3(str2); 

cout << "str1 是否為空:" << str1.empty() << endl;
cout << "str1 長度: " << str1.size() << endl;
cout << "str2 長度: " << str2.size() << endl;
cout << "str3 長度: " << str3.size() << endl;
cout << "str3 長度: " << str3.length() << endl;

if(str1 == str2)cout << "str1 與 str2 內容是否相同:Yes" << endl;
else cout << "str1 與 str2 內容是否相同:No" << endl;


if(str2 == str3)cout << "str2 與 str3 內容是否相同:Yes" << endl;
else cout << "str2 與 str3 內容是否相同:No" << "\n";

str1 是否為空:1
str1 長度: 0
str2 長度: 5
str3 長度: 5
str1 與 str2 內容是否相同:No
str2 與 str3 內容是否相同:Yes

string str3(str2); 
string str3 = str2;

=>

輸入整行字串:getline

  • 當接收一個字串時,可以接收空格  

#include <iostream>
using namespace std;

int main()
{
    string name;
    cout << "Please, enter your full name: ";
    getline (cin,name);
    cout << "Hello, " << name << "!\n";

    return 0;
}

Please, enter your full name: jason chen
Hello, jason chen!

實作

提示 :

先確認字串長度

再判斷字串s頭尾是否相同

#include<iostream>
#include<string>
using namespace std;

int main(){
    string s;bool c=1;
    cin >> s;
    for(int i=0,len=s.size(); i<len; i++){
        
        if(s[i]!=s[len-1-i]){ //判斷字串s頭尾是否相同
            c=0;
            break;
        }
    }
    
    if(c) cout << "yes" << endl;
    else cout << "no" << endl;
    
    return 0;
}

提示 :

使用ASCII碼的觀念 : 0比9的ASCII小

不考慮輸入空格的情況 (用cin輸入)
引入 <string> 函式庫,使用 size() 取長度

#include<iostream>
#include<string>
using namespace std;

int main(){
    //K==7
    string p, ans;
    cin >> p; 
    for(int i=0,len=p.size(); i<len; i++){
        ans += char(p[i]-7);
    }
    cout << ans << endl;
}

提示:

  • 引入 <math.h> 函式庫後,可以使用 abs() 取絕對值
  • 將輸入的數字以字串處理,方便取各個位數的值
#include<iostream>
#include<math.h>
using namespace std;

int main(){
    string p;
    int A = 0, B = 0;
    cin >> p;
    for(int i=0,len=p.size(); i<len; i++){
        
        if(i%2) A += p[i]-'0';
        else B += p[i]-'0';
        //最後是取絕對值,所以重點是交錯取,A是奇數位總和或偶數位總和不會造成影響
        
    }
    cout<< abs(A - B) <<endl;
}

為什麼要 -'0'

cin 讀進來的字串(或字元),

每個數字其實是以 字元(char)型態 儲存的,

例如:

string p = "263541";
char ch = p[0];  // ch = '2'

你以為是數字 2,其實是字元 '2',它的 ASCII 值是 50

所以:

就會變成:

這樣就可以把字元 '2' 轉為整數 2,開始進行數學運算

p[i] - '0'
'2' - '0' = 50 - 48 = 2
'6' - '0' = 54 - 48 = 6

解答中這段程式碼:

A += p[i] - '0';
B += p[i] - '0';

意思是:「把字串中的字元 char 轉成數值 int 後加總」

kahoot!

Made with Slides.com