sunfrancis12
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Ch08 字元字串處理 > 上一章: [Ch07 陣列]( https://hackmd.io/@sunfrancis12/BJ__b0sz6) > 下一章: [Ch09 函式與遞迴]( https://hackmd.io/@sunfrancis12/BykpM0iza) > 回目錄: [NTCU程式戰鬥營C講義](https://hackmd.io/@sunfrancis12/ByfdXdjG6) ## 字元 Char 字元`Char` 是一種變數型態,專門儲存個各種字(`*`,`%`,`a`,`?`) 在程式的世界裡面又把上述的那些字稱作**字元** char變數只能儲存一個字元: ```c= char c = 'a'; //創建一個名稱為c的char,並初始化値'a' char s = '&'; //創建一個名稱為c的char,並初始化値'&' char abc = ':'; //創建一個名稱為abc的char,並初始化値':' ``` **Char的資料型態** | 資料型態 | 格式化字串 | 變數範圍 | | -------- | ---------- | --------------------- | | char | %c | -128至127 (ASCII編碼) | 輸出三個字元: ```c= #include<stdio.h> int main(){ char c = 'a'; //創建一個名稱為c的char,並初始化値'a' char s = '&'; //創建一個名稱為c的char,並初始化値'&' char abc = ':'; //創建一個名稱為abc的char,並初始化値':' printf("char c: %c\n",c); printf("char s: %c\n",s); printf("char abc: %c",abc); } ``` 輸出結果: ```c= char c: a char s: & char abc: : ``` 使用`scanf()`輸入字元並輸出: ```c= #include<stdio.h> int main(){ char a; printf("input char a: "); scanf("%c",&a); printf("char a: %c\n",a); } ``` 輸入內容: ```c= g ``` 輸出內容: ```c= input char a: g char a: g ``` 除了scanf()以外,我們也可以使用其他的函式來完成字元的輸入輸出 * `getchar()` 輸入一個字元 * `putchar()` 輸出一個字元 ```c= #include<stdio.h> int main(){ char a; printf("input a: "); a = getchar(); //義同於scanf("%c",&a); printf("a value: "); putchar(a); //義同於prinf("%c",a); } ``` 輸入內容: ```c= c ``` 輸出結果: ```c= input a: c a value: c ``` ### 特殊字元 特殊字元跟我們常見的字母、數字和符號等字元不太一樣 他們會用於一些特殊的情況,比如換行`\n`、空字元`\0` 他們的格式都是以`\`作為開頭 常見的特殊字元: * 換行: `\n` * 空字串: `\0` * tab: `\t` * 字元`"`: `\"` * 字元`\`: `\\` <a style="color:orange">【例題】</a> > 請寫一個程式,計算在輸入換行("\n")前總共輸入了多少字元,並將結果輸出 > > *註: 換行就是指當鍵盤按下Enter* 範例輸入: ```c= apple ``` 範例輸出: ```c= 總共輸入了5個字元 ``` 範例程式: ```c= #include<stdio.h> int main(){ char input; int sum=0; while(input = getchar()){ if(input == '\n') break; sum++; } printf("總共輸入了%d個字元",sum); } ``` ## Char的本質 ASCII 在電腦(計算機)的世界裡是沒有**字元**這個概念的,如果想要讓電腦紀錄字元的話,我們需要把字元表示成一個整數 為此我們有了ASCII TABLE,這個圖表上的每個字元都有相對應的一個整數 ![image.png](https://hackmd.io/_uploads/rkL2DWqQ6.png) 輸出字元的ASCII値: ```c= #include<stdio.h> int main(){ char ascii='a'; printf("ascii value: %d\n",ascii); printf("ascii value 97: %c",97); } ``` 輸出結果: ```c= ascii value: 97 ascii value 97: a ``` ## String 字串 上面介紹了字元,一次可以記錄一個字 如果我們今天要記錄一個句子的話,那我們就必須記錄許多個字元 因此我們需要建立一個**字元的陣列**,也就是**字串** ```c= char c ='A'; //建立一個字元c char s[20] = "Apple" //建立一個長度為20的字串s ``` :::info 字元的初始化要使用單引號`'`,而字串的話則是雙引號`"` ::: 要輸入一個字串的話,我們可以使用格式化字串`%s` ```c= #include<stdio.h> int main(){ char str[20]; scanf("%s",str); printf("你輸入的字串為: %s",str); } ``` 輸入內容: ```c= banana hello how are you ``` 輸出結果: ```c= 你輸入的字串為: banana 你輸入的字串為: hello ``` :::info scanf("%s")的話遇到空格就會輸入完成,空格後面的字元不會被輸入 ::: 如果希望遇到空格還可以繼續輸入的話可以使用`gets()`,他會持續輸入直到遇到換行`\n`為止 * `gets()`: 輸入一個字串,且遇到`\n`才會停止輸入 * `puts()`: 輸出一個字串 ```c= #include<stdio.h> int main(){ char str[30]; gets(str); printf("你輸入的字串為: "); puts(str); } ``` 輸入內容: ```c= i love you ``` 輸出結果: ```c= 你輸入的字串為: i love you ``` ## 字串處理 在C語言裡有`string.h`的函式庫,裡面有許多常用於字串處理的工具 ### `String.h` `string.h`是C標準庫的標頭檔,其中包含了宏(巨集)定義、常數以及函數和類型的聲明,涉及的內容除了字串處理之外,還包括大量的記憶體處理常式 > [維基百科 -String.h](https://zh.wikipedia.org/zh-tw/String.h) **<string.h>的實用函數:** * `size_t strlen(const char *)`: 返回一個字串的長度 - strlen("15ff") => 4 * `char *strcpy(char* str1, const char* str2)`: 將str2拷貝給str1 - strcpy(a,b) => 等同於a = b的值 * `char *strcat(char *dest, const char *src)`: 在字串dest之後連接上src - strcat(a,b) => 等同於 a = a + b * `int strcmp(const char *, const char *)`: 基於字典順序比較兩個字串 - 如果返回值<0,則表明str1小於str2 - 如果返回值,如果> 0,則表明str2 小於 str1 - **如果返回值= 0,則表明str1 等於str2** > [取自 strcmp() -GITBOOK.NET](https://tw.gitbook.net/c_standard_library/c_function_strcmp.html) 上述`string.h`函式的範例程式: ```c= #include<stdio.h> #include<string.h> int main(){ char str1[20]="hi",str2[20]="hello"; printf("str2的字串長度為: %d\n",strlen(str2)); printf("str1: %s,str2: %s\n",str1,str2); strcat(str1,str2); printf("\n執行strcat(str1,str2)...\n"); printf("str1: %s,str2: %s\n",str1,str2); strcpy(str1,str2); printf("\n執行strcpy(str1,str2)...\n"); printf("str1: %s,str2: %s\n",str1,str2); printf("\n比較str1,str2是否相同...\n"); printf("strcmp(str1,str2): %d\n",strcmp(str1,str2)); if(strcmp(str1,str2)==0) printf("str1和str2相同"); else printf("str1和str2不相同"); } ``` 輸出結果: ```c= str2的字串長度為: 5 str1: hi,str2: hello 執行strcat(str1,str2)... str1: hihello,str2: hello 執行strcpy(str1,str2)... str1: hello,str2: hello 比較str1,str2是否相同... strcmp(str1,str2): 0 str1和str2相同 ``` <a style="color:orange">【例題】</a> > 輸入一個字串,並將其翻轉後的結果輸出 範例輸入: ```c= nice day how are you? ``` 範例輸出: ```c= 原始字串: nice day how are you? 翻轉後的字串: ?uoy era woh yad ecin ``` 範例解答: ```c= #include<stdio.h> #include<string.h> int main(){ char str[30]; gets(str); printf("原始字串: %s\n",str); printf("翻轉後的字串: "); for(int i=strlen(str)-1;i>=0;i--){ printf("%c",str[i]); } } ``` <a style="color:orange">【例題】</a> > 程上題,只有單字的位置翻轉,文字順序並未改變 範例輸入: ```c= nice day how are you ``` 範例輸出: ```c= 原始字串: nice day how are you 翻轉後的字串: you are how day nice ``` 範例程式: ```c= #include<stdio.h> #include<string.h> int main(){ char str[100]; gets(str); printf("原始字串: %s\n",str); printf("翻轉後的字串: "); for(int i=strlen(str)-1;i>=-1;i--){ if(str[i]==' ' || i==-1){ for(int j=i+1;str[j]!=' '&& j<strlen(str);j++) putchar(str[j]); putchar(' '); } } } ``` <a style="color:orange">【例題】</a> > 請判斷輸入的文字是否為"迴文"(字串的每個字元都前後對稱) 範例輸入: ```c= chjgffgjhc asdfghjkllkjhgfdsa appleapple ``` 範例輸出: ```c= 輸入的字串: chjgffgjhc 字串: chjgffgjhc是迴文 輸入的字串: asdfghjkllkjhgfdsa 字串: asdfghjkllkjhgfdsa是迴文 輸入的字串: appleapple 字串: appleapple不是迴文 ``` 範例程式: ```c= int main(){ char str[100]; gets(str); printf("輸入的字串: %s\n",str); printf("字串: %s"); int sum=0; for(int i=0;i<=(strlen(str)-1)/2;i++){ if(str[i] != str[strlen(str)-1-i]) sum++; } if(sum>0) printf("不是迴文"); else printf("是迴文"); } ``` <a style="color:orange">【例題】</a> > 請將輸入的字串的小寫字母轉成大寫字母,並輸出 範例輸入: ```c= Apple BAnasdin sidFFjnSJ ASD MAmaLiLijJDUfSAFudDG^(*&@#@HGR*9 3hd7g328) ``` 範例輸出: ```c= 輸入的字串: Apple 轉換後的字串: APPLE 輸入的字串: BAnasdin sidFFjnSJ ASD 轉換後的字串: BANASDIN SIDFFJNSJ ASD 輸入的字串: MAmaLiLijJDUfSAFudDG^(*&@#@HGR*9 3hd7g328) 轉換後的字串: MAMALILIJJDUFSAFUDDG^(*&@#@HGR*9 3HD7G328) ``` 範例程式: ```c= #include<stdio.h> #include<string.h> int main(){ char str[100]; gets(str); printf("輸入的字串: %s\n",str); for(int i=0;i<strlen(str);i++){ if('a'<=str[i] && str[i]<='z') str[i] -= 32; } printf("轉換後的字串: %s"); } ``` --- > 上一章: [Ch07 陣列]( https://hackmd.io/@sunfrancis12/BJ__b0sz6) > 下一章: [Ch09 函式與遞迴]( https://hackmd.io/@sunfrancis12/BykpM0iza) > 回目錄: [NTCU程式戰鬥營C講義](https://hackmd.io/@sunfrancis12/ByfdXdjG6)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully