Akashi Sekizaki
    • 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
    # プログラミング基礎WS 解説 ## 多重ループとポインタ ### 【細分化】 ![](https://i.imgur.com/ROBxD7J.png) ### 【手順】 <注意> 1. char型の文字列の文字数は1文字以上50文字以下と仮定する 2. ” char A[100]; ” のように領域を確保する目的にのみ、配列の宣言を行ってよい 3. 文字列内の文字を参照するときは、ポインタを使わなくてはならない 4. 読み取る文字列はアルファベットのみとし、大文字と小文字は別の文字として扱う ### 1.キーボードから文字列を読み込む * 1-1. char型で領域を宣言(50文字分の大きさ) * 1-2. gets関数で入力されたものを読み取る * 1-3. 1-1・1-2をもう一回行う ``` #include <stdio.h> int main(void){ //領域確保 char a[50]; char b[50]; //読み取り gets(a); gets(b); /* 読み取りが出来たかを確認する puts(a); puts(b); */ return 0; } ``` #### 補足 ・今回は、初めに読み取った文字列をa, 2つ目に読み取った文字列をbとした ・<注意>より、1-1で領域を50文字分の大きさにした ・gets関数を使ったのは、一行分をまとめて読み込むため ### 2.「初めに読み取った文字列内の文字」が「2つ目に読み取った文字列内の文字」に含まれているかを確認し、含まれていた文字数を数える ☆この説明が難しいと感じたら、「3.おまけ」をご覧ください! #### 2-1.「含まれていた文字数」をカウント変数を用意する * 2-1-1. int型の変数countを宣言する。 * 2-1-2. countを0で初期化する ``` int count = 0; ``` #### 2-2. 「初めに読み取った文字列(今回はa)」のポインタの先頭から順番に、ループを使って値を参照する * 2-2-1. for文で 0番 から strlen(a)-1番目 まで繰り返す * 2-2-2. `*(a+i)`(iはforの繰り返した回数) によって、「初めに読み取った文字列内の文字」を1文字参照できる #### 2-3. 上記のループの中で、「2つ目に読み取った文字列(今回はb)」のポインタの先頭から順番に、ループを使って値を参照し、2-2の「初めに読み取った文字列内の文字」と比較する * 2-3-1. 2-2-1のfor文内で、0番 から strlen(b)-1番目 までfor文で繰り返す * 2-3-2. `*(b+j)`(jはforの繰り返した回数) によって、「2つ目に読み取った文字列の文字」を1文字参照できる * 2-3-3. `*(a+i) == *(b+j)`のとき、countを1増やす * 2-3-4. 「(含まれていた文字数) = n」(nは、countの値)の形で出力する ``` #include <stdio.h> #include <string.h> int main(void){ char a[50]; //①を読み込むための領域確保 char b[50]; //②を読み込むための領域確保 gets(a); //①を読み込む gets(b); //②を読み込む int count = 0;//含まれていた文字数(一致した回数)を保存する変数 for(int i = 0; i < strlen(a); i++){ //①で参照する文字を動かす for(int j = 0; j < strlen(b); j++){ //②で参照する文字を動かす if(*(a + i) == *(b + j)){ count++; //一致したら、countを1ふやす } } } printf(" (含まれていた文字数) = %d\n", count);//出力 return 0; } ``` となり、実装が完了した。 #### 補足 ・strlen(a)を使うには、#include <string.h>が必要である ・%dでprintfのなかにint型の変数の値をいれることができる ## 3.おまけ ~二重for文を実装するまでの詳しい解説~ ☆2-2, 2-3で難しいと感じた人や、解答のコードでうまく行くのは分かったが、なぜfor文のなかにfor文を入れるのか分からない・今後自分で実装できる自信がない人のために、考え方の流れを解説していきます! > 「文字列」と「1文字」が一致しているか確かめよ という問題を考えるとする。配列を使っていいなら、 ``` #include <stdio.h> #include <string.h> int main(void){ char a[5] = "ABC"; char b = 'A'; int count = 0; for(int i = 0; i < strlen(a); i++){ if(a[i] == b){ count++; } } printf("count = %d\n", count); return 0; } ``` となる。 これをgetsを使い文字を読み取り(同じくABCとA)、ポインタで値を参照すると、 ``` #include <stdio.h> #include <string.h> int main(void){ char a[50]; char b[50]; gets(a); gets(b); int count = 0; for(int i = 0; i < strlen(a); i++){ if(*(a+i) == *b){ count++; } } printf("count = %d\n", count); return 0; } ``` となる。 if文の`*(a+i)`によって、A→B→Cと動かしている。 そして、`if(*(a+i) == *b)`で、aのAとbのA・aのBとbのA・aのCとbのA というように 「文字列内の文字」と「文字」を1文字ずつ比べている。 これを踏まえて、本来の問題を見ると、 > 「初めに読み取った文字列内の文字」と「2つ目に読み取った文字列内の文字」を一文字ずつ比べ~~~ とある。 今回のような問題では、図のように「文字列aの0文字目と文字列bのすべての文字(0番, 1番, ... , strlen(b)-1番目)」, 「文字列aの1文字目と文字列bのすべての文字」, ... , 「文字列strlen(a)-1の文字目と文字列bのすべての文字」のようにすべての文字を一回ずつ比べる方法が最も基本的な書き方である。 それを図にしたのが、下である。 ![](https://i.imgur.com/DzsTewD.png) 図にすると、文字列aの0番と、文字列bのstrlen(b)-1番を比べたあと、0番に戻った文字列bと文字列aの1番を比べている*ことが確認できる。 この状況*をコードにすると、 ``` i = 0; //文字列aの0番の文字と文字列bの文字を0番からstrlen(b)-1番まで比べる for(int j = 0; j < strlen(b); j++){ if(*(a+i) == *(b+j)){ count++; } } i++; //文字列aの1番の文字と文字列bの文字を0番からstrlen(b)-1番まで比べる for(int j = 0; j < strlen(b); j++){ if(*(a+i) == *(b+j)){ count++; } } i++ //文字列aの2番と文字列bの文字を0番からstrlen(b)-1番まで比べる .... ``` このように動いている。 つまり、iが1増えるごとに ``` //文字列aのi番と文字列bの文字を0番からstrlen(b)-1番まで比べる for(int j = 0; j < strlen(b); j++){ if(*(a+i) == *(b+j)){ count++; } } ``` を実行することで図のように動くコードを実装できる。 これをstrlen(a)回繰り返すので、下記の「iが0~strlen(a)-1まで繰り返すコード」である ``` for(int i = 0; i < strlen(a); i++){ //☆ } ``` の☆に ``` //文字列aのi番と文字列bの文字を0番からstrlen(b)-1番まで比べる for(int j = 0; j < strlen(b); j++){ if(*(a+i) == *(b+j)){ count++; } } ``` をいれたコードである ``` #include <stdio.h> #include <string.h> int main(void){ char a[50]; char b[50]; gets(a); gets(b); int count = 0; for(int i = 0; i < strlen(a); i++){ for(int j = 0; j < strlen(b); j++){ if(*(a + i) == *(b + j)){ count++; } } } printf(" (含まれていた文字数) = %d\n", count); return 0; } ``` が図のように動くコードであり、2.で示した解答である。

    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