作业帮 > 英语 > 作业

用PHP 计算出文章中各个字母出现的次数

来源:学生作业帮 编辑:作业帮 分类:英语作业 时间:2024/05/22 22:55:36
用PHP 计算出文章中各个字母出现的次数
Ideas abound,but none seem to hold up to scientific scrutiny.One is that yawning helps to cool the brain by increasing blood flow to the jaws,neck,and sinuses,and then removing heat from this blood when inhaling a big breath.Counterintuitively,yawning occurs less frequently in hot weather,when air has less ability to cool the body.In short,yawning "fails precisely when we need it," Dr.Adrian Guggisberg told WebMD.One hypothesis that has not (yet) been discarded:yawns "serve as a signal for our bodies to perk up,a way of making sure we stay alert," Maria Konnikova wrote in The New Yorker."A yawn is usually followed by increased movement and physiological activity,which suggests that some sort of 'waking up' has taken place."
计算出这篇文章中各个字母出现的次数.要有过程和详细的注释.
<?php

$String = 'Ideas abound, but none seem to hold up to scientific scrutiny. One is that yawning helps to cool the brain by increasing blood flow to the jaws, neck, and sinuses, and then removing heat from this blood when inhaling a big breath. Counterintuitively, yawning occurs less frequently in hot weather, when air has less ability to cool the body. In short, yawning "fails precisely when we need it," Dr. Adrian Guggisberg told WebMD. One hypothesis that has not (yet) been discarded: yawns "serve as a signal for our bodies to perk up, a way of making sure we stay alert," Maria Konnikova wrote in The New Yorker. "A yawn is usually followed by increased movement and physiological activity, which suggests that some sort of \'waking up\' has taken place';

$ZhiMu  = 'abcdefghijklmnopqrstuvwxyz';

for( $i = 0; $i < strlen( $ZhiMu ); $i++ ) { //循环遍历26个字母, 也就是一个一个字母比对

 $s = $ZhiMu{$i}; //得到要比对的字母

 $b = 0; //初始化字母次数为0

 for( $j = 0; $j < strlen( $String ); $j++ ) { //循环遍历要比对的字符串, 也就是从头开始一个一个比对

  if ( $s == $String{$j} ) { //如果两个字母相同

   $b += 1; //次数加上1

  }

 }

 echo $ZhiMu{$i}, '出现的次数是:', $b, '<br/>'; //输出结果

}
不过这个好像是个土办法,
研究了下,还是用下面的代码简单些:
<?php

$String = 'Ideas abound, but none seem to hold up to scientific scrutiny. One is that yawning helps to cool the brain by increasing blood flow to the jaws, neck, and sinuses, and then removing heat from this blood when inhaling a big breath. Counterintuitively, yawning occurs less frequently in hot weather, when air has less ability to cool the body. In short, yawning "fails precisely when we need it," Dr. Adrian Guggisberg told WebMD. One hypothesis that has not (yet) been discarded: yawns "serve as a signal for our bodies to perk up, a way of making sure we stay alert," Maria Konnikova wrote in The New Yorker. "A yawn is usually followed by increased movement and physiological activity, which suggests that some sort of \'waking up\' has taken place';

$ZhiMu  = 'abcdefghijklmnopqrstuvwxyz';

for( $i = 0; $i < strlen( $ZhiMu ); $i++ ) { //循环遍历26个字母, 也就是一个一个字母比对

 echo $ZhiMu{$i}, '出现的次数是:', substr_count( $String, $ZhiMu{$i} ), '<br/>'; //输出结果

}