要求

给出一个字符串如 “A journey of, a thousand 'miles' must can't \"begin\" with a single step.” ,通过 PHP 程序处理变成 “a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP.”

注意:
1、每个单词最后的字符如果是大写就变成小写,如果是小写就变成大写。
2、需要考虑类似  can't 这种形式的转换。
3、标点符号(只考虑 , ' " . ;)不用变化。

参考算法

<?php

    function convertLastChar($str) {
$markArr = array(", ", "' ", "\" ", ". ", "; ");
$ret = "";
for ($i = 0, $j = strlen($str); $i < $j; $i++) {
if ($i < $j - 2) {
$afterStr = $str{$i + 1} . $str{$i + 2};
} else if ($i < $j - 1) {
$afterStr = $str{$i + 1} . " ";
}
if (in_array($afterStr, $markArr)
|| $i == $j - 1
|| $str{$i + 1} == " ") {
$ret .= strtoupper($str{$i}) === $str{$i}
? strtolower($str{$i})
: strtoupper($str{$i});
} else {
$ret .= $str{$i};
}
}
return $ret;
} ?>

测试

<?php

    //test
$str1 = "A journey of, a thousand 'miles' must can't \"begin\" with a single step.";
$str2 = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. ";
$str3 = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a ";
$str4 = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a B";
$str5 = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a b'";
$str6 = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a B\""; echo "source:<br/>" . $str1 . "<br/>result:<br/>" . convertLastChar($str1) . "<br/><br/>";
echo "source:<br/>" . $str2 . "<br/>result:<br/>" . convertLastChar($str2) . "<br/><br/>";
echo "source:<br/>" . $str3 . "<br/>result:<br/>" . convertLastChar($str3) . "<br/><br/>";
echo "source:<br/>" . $str4 . "<br/>result:<br/>" . convertLastChar($str4) . "<br/><br/>";
echo "source:<br/>" . $str5 . "<br/>result:<br/>" . convertLastChar($str5) . "<br/><br/>";
echo "source:<br/>" . $str6 . "<br/>result:<br/>" . convertLastChar($str6) . "<br/><br/>";
?>

结果:

source:
A journey of, a thousand 'miles' must can't "begin" with a single step.
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. source:
A journey of, a thousand 'miles' must can't "begin" with a single step.
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. source:
A journey of, a thousand 'miles' must can't "begin" with a single step. a
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A source:
A journey of, a thousand 'miles' must can't "begin" with a single step. a B
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A b source:
A journey of, a thousand 'miles' must can't "begin" with a single step. a b'
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A B' source:
A journey of, a thousand 'miles' must can't "begin" with a single step. a B"
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A b"

我们可以看到,是符合预期的。

题目来源

http://blog.sijiaomao.com/?p=98,有改动(增加了can't这种),按改后的规则,原文答案全是错的。

PHP字符串word末字符大小写互换的更多相关文章

  1. javascript 字符串 数字反转 字母大小写互换

    // 符串abcd123ABCD456 怎么转换为 ABCD321abcd654 // 数字要倒序 小写转大写, 大写转小写 Array.prototype.reverse = function() ...

  2. JS大写转小写小写转大写,JS字符串大小写互换

    Array.prototype.map.call(str,a=>a.toUpperCase(a)==a?a.toLowerCase():a.toUpperCase()).join(''); 效果 ...

  3. 大小写互换-"数字字符串"转换成数字

    今天穿着hacker浑浊马甲在百度编程课堂实训习题中发现了这个很简单的问题,就做了下. 为了考虑输入的是否是数字,结果写好后竟然超时了. 不过里面用到的将字符串装换成数字的方法,感觉是个收获,因此在此 ...

  4. lintcode :sort letters by case字符大小写排序

    题目 字符大小写排序 给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序. 您在真实的面试中是否遇到过这个题? Yes 样例 给出"abAcD",一个可能的答案为& ...

  5. C/C++中字符串String及字符操作方法

    本文总结C/C++中字符串操作方法,还在学习中,不定期更新. .. 字符串的输入方法 1.单个单词能够直接用std::cin,由于:std::cin读取并忽略开头全部的空白字符(如空格,换行符,制表符 ...

  6. 用Java编程找到两个字符串中共有的字符

    这道题的算法思想是把字符串1中的每个字符与字符串2中的每个字符进行比较,遇到共同拥有的字符,放入另一个数组中,最后顺序输出即可 但是这道题的难点在于怎么排除重复的字符 public class bot ...

  7. 【LintCode】判断一个字符串是否包含另一个字符串的所有字符

    问题描述: 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母. 样例 给出 A = "ABCD" B = "ACD",返 ...

  8. SQL:将字符串以特定字符分割并返回Table

    split 语法 ALTER FUNCTION [dbo].[F_SPLIT] ( @str VARCHAR(MAX) , ) ) /********************************* ...

  9. JAVA----编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符

    package com.pb.demo.packclass.demo1; import java.util.HashSet; /** * 编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符 ...

随机推荐

  1. Windows聚焦转为图片

    1.windows聚焦图片目录路径: C:\Users\Er\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1 ...

  2. IOS 解析Json数据(NSJSONSerialization)

    ● 什么是JSON ● JSON是一种轻量级的数据格式,一般用于数据交互 ● 服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除 外) ● JSON的格式很像OC中的字典和数组 ...

  3. ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id

    远程删除key ssh-keygen -f "~/.ssh/known_hosts" -R 192.168.0.34 如果还是不可以,通过 ssh-keygen 重新生成key

  4. BZOJ 3233: [Ahoi2013]找硬币

    BZOJ 3233: [Ahoi2013]找硬币 标签(空格分隔): OI-BZOJ OI-DP Time Limit: 10 Sec Memory Limit: 64 MB Description ...

  5. JS面向对象、prototype、call()、apply()

    一. 起因 那天用到prototype.js于是打开看看,才看几行就满头雾水,原因是对js的面向对象不是很熟悉,于是百度+google了一把,最后终于算小有收获,写此纪念一下^_^. prototyp ...

  6. leetcode_No.1 Two Sum

    原题: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  7. Bean的初始化和销毁

    在我们实际开发的时候,经常会遇到在Bean在使用之前或者之后做些必要的操作,Spring对Bean的生命周期的操作提供了支持.在使用Java配置和注解配置下提供如下两种方式.    1.Java配置方 ...

  8. matlab linux下无界面运行

    今日做吸引域的仿真,由于需要遍历100*100*100的空间,需要的时间比较长,发现程序没运行一段时间,就会出现Out of memory的错误,而且出错的部分在于截取figure内部图片的部分. 开 ...

  9. 读取当前路径,列出xls文件

    import java.io.File; public class GetCurrentDirectory { public String GetDirectory() { File director ...

  10. 友盟分享小结 - iOS

    因之前都写在了 AppDelegate 类中,看起来过于臃肿,此次基于友盟分享重新进行了一次优化,此次分享内容基于已经成功集成 SDK 后 code 层级部分.注:此次分享基于 SDK 6.9.3,若 ...