zoukankan      html  css  js  c++  java
  • 如何實現输入字符串This is an Apple on eBay 输出 Siht si na Elppa no yAbe

     1 <?php
     2 $str = "This is an Apple on eBay"; //定义字符串
     3 $len = strlen($str); //字符串长度
     4 $sup = []; //空数组,用来记录大写字母的位置
     5 //循环字符串
     6 for ($i=0;$i<$len;$i++){
     7     //如果当前字母是大写,那么转换为小写后,前后不相等
     8     if($str[$i] != strtolower($str[$i])){
     9         $sup[] = $i; //记录大写的位置
    10     }
    11 }
    12 //print_r($sup);
    13 // 字符串分割成数组
    14 $arr = explode(" ","This is an Apple on eBay");
    15 //空字符串,存储转换后的结果
    16 $new_str = "";
    17 //循环数组,对元素进行处理
    18 foreach ($arr as $k=>$v){
    19     //反转并转换成小写,用空格拼接
    20     $new_str .= strtolower(strrev($v))." ";
    21 }
    22 //去掉尾部的空格
    23 $new_str = trim($new_str);
    24 //循环大写的位置数组
    25 foreach ($sup as $v) {
    26     //将新的字符串所在的位置转换成大写
    27     $new_str[$v] = strtoupper($new_str[$v]);
    28 }
    29 
    30 echo $new_str;
    31 //Siht si na Elppa no yAbe
    32 ?>
  • 相关阅读:
    image对象
    Frame/IFrame 对象
    Form 对象
    JavaScript 对象 实例
    button对象
    正则介绍以及多种使用方法
    js /jquery停止事件冒泡和阻止浏览器默认事件
    一些兼容性的知识
    面试题总结
    事件
  • 原文地址:https://www.cnblogs.com/songbao/p/11272803.html
Copyright © 2011-2022 走看看