0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

同じ文字を2回入力したときだけ「YES」、同じ文字じゃない、または3回だと「NO」

Posted at

課題内容

同じ文字を2回入力したときだけ「YES」、
同じ文字じゃない、または3回だと「NO」と
結果が出力されるプログラムを作成してください。

ソースコード

<?php
// Your code here!
//ここに評価したい文字を入力
$str = "hogehoge";
//ここに評価したい文字の頭文字を入力
$moji = mb_substr($str,0,1);
// ","(カンマ)で分割
$parts = explode($moji, $str);



unset($parts[0]);
$i = count($parts);



//区切った文字列を配列に格納
for($o=1; $o <= $i; $o++){
    $array[$o] = $moji.$parts[$o]."\n";
    //print($array[$o]);
}

//偶数か奇数か判定
if($i%2==0) {
    for($o=1; $o <= $i; $o=$o+2){
        $k = $o + 1;
        if($array[$o] !=  $array[$k]){
            echo("NO");
            exit;
        }
    //ここまで回れば偶数かつ同じ文字との比較に成功
    }
    echo("YES");
} else { 
    echo("NO");
    exit;
}

?>
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?