2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【C#】文字列をchar型のデータにして文字比較をする

2
Posted at

はじめに

文字列を1文字ずつ取得し、検索対象の文字があるかを検索するプログラムについて実装したので記事にします。

問題

文字列 S と文字 c が与えられるので、 c は S の何文字目に現れるかを調べてください。

実装したソースコード

実装したソースコードは下記の通りです

using System;

class Program
{
    static void Main()
    {
        // 自分の得意な言語で
        // Let's チャレンジ!!
        var s = Console.ReadLine();
        char t = Console.ReadLine()[0];
        int l = s.Length;
        for(int i=0;i<l;i++){
            char c = s[i];
            if(c == t){
                Console.WriteLine(i+1); 
            }
        }
        
    }
}

実行結果

実行結果は以下の通りです

ayaka.jpg

文字列「ayaka」から「k」の文字は何文字目に現れるか調べると、左から4文字目に現れました。

最後に

C#で指定された文字列から指定された文字が何番目に現れるかのプログラムを実装しました。

2
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?