<h1 class="lead">要求
给出一个字符串如 “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、标点符号(只考虑,' " . ;)不用变化。
参考算法
</span><span style="color: #0000ff;">function</span> convertLastChar(<span style="color: #800080;">$str</span><span style="color: #000000;">) {
</span><span style="color: #800080;">$markArr</span> = <span style="color: #0000ff;">array</span>(",","' ","\" ",". ","; "<span style="color: #000000;">);
</span><span style="color: #800080;">$ret</span> = ""<span style="color: #000000;">;
</span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0,<span style="color: #800080;">$j</span> = <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$str</span>); <span style="color: #800080;">$i</span> < <span style="color: #800080;">$j</span>; <span style="color: #800080;">$i</span>++<span style="color: #000000;">) {
</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$i</span> < <span style="color: #800080;">$j</span> - 2<span style="color: #000000;">) {
</span><span style="color: #800080;">$afterStr</span> = <span style="color: #800080;">$str</span>{<span style="color: #800080;">$i</span> + 1} . <span style="color: #800080;">$str</span>{<span style="color: #800080;">$i</span> + 2<span style="color: #000000;">};
} </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$i</span> < <span style="color: #800080;">$j</span> - 1<span style="color: #000000;">) {
</span><span style="color: #800080;">$afterStr</span> = <span style="color: #800080;">$str</span>{<span style="color: #800080;">$i</span> + 1} . " "<span style="color: #000000;">;
}
</span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">in_array</span>(<span style="color: #800080;">$afterStr</span>,<span style="color: #800080;">$markArr</span><span style="color: #000000;">)
</span>|| <span style="color: #800080;">$i</span> == <span style="color: #800080;">$j</span> - 1
|| <span style="color: #800080;">$str</span>{<span style="color: #800080;">$i</span> + 1} == " "<span style="color: #000000;">) {
</span><span style="color: #800080;">$ret</span> .= <span style="color: #008080;">strtoupper</span>(<span style="color: #800080;">$str</span>{<span style="color: #800080;">$i</span>}) === <span style="color: #800080;">$str</span>{<span style="color: #800080;">$i</span><span style="color: #000000;">}
</span>? <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$str</span>{<span style="color: #800080;">$i</span><span style="color: #000000;">})
</span>: <span style="color: #008080;">strtoupper</span>(<span style="color: #800080;">$str</span>{<span style="color: #800080;">$i</span><span style="color: #000000;">});
} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #800080;">$ret</span> .= <span style="color: #800080;">$str</span>{<span style="color: #800080;">$i</span><span style="color: #000000;">};
}
}
</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$ret</span><span style="color: #000000;">;
}
?>
测试
</span><span style="color: #008000;">//</span><span style="color: #008000;">test</span>
<span style="color: #800080;">$str1</span> = "A journey of,a thousand 'miles' must can't \"begin\" with a single step."<span style="color: #000000;">;
</span><span style="color: #800080;">$str2</span> = "A journey of,a thousand 'miles' must can't \"begin\" with a single step. "<span style="color: #000000;">;
</span><span style="color: #800080;">$str3</span> = "A journey of,a thousand 'miles' must can't \"begin\" with a single step. a "<span style="color: #000000;">;
</span><span style="color: #800080;">$str4</span> = "A journey of,a thousand 'miles' must can't \"begin\" with a single step. a B"<span style="color: #000000;">;
</span><span style="color: #800080;">$str5</span> = "A journey of,a thousand 'miles' must can't \"begin\" with a single step. a b'"<span style="color: #000000;">;
</span><span style="color: #800080;">$str6</span> = "A journey of,a thousand 'miles' must can't \"begin\" with a single step. a B\""<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "source:<br/>" . <span style="color: #800080;">$str1</span> . "<br/>result:<br/>" . convertLastChar(<span style="color: #800080;">$str1</span>) . "<br/><br/>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "source:<br/>" . <span style="color: #800080;">$str2</span> . "<br/>result:<br/>" . convertLastChar(<span style="color: #800080;">$str2</span>) . "<br/><br/>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "source:<br/>" . <span style="color: #800080;">$str3</span> . "<br/>result:<br/>" . convertLastChar(<span style="color: #800080;">$str3</span>) . "<br/><br/>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "source:<br/>" . <span style="color: #800080;">$str4</span> . "<br/>result:<br/>" . convertLastChar(<span style="color: #800080;">$str4</span>) . "<br/><br/>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "source:<br/>" . <span style="color: #800080;">$str5</span> . "<br/>result:<br/>" . convertLastChar(<span style="color: #800080;">$str5</span>) . "<br/><br/>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "source:<br/>" . <span style="color: #800080;">$str6</span> . "<br/>result:<br/>" . convertLastChar(<span style="color: #800080;">$str6</span>) . "<br/><br/>"<span style="color: #000000;">;
?>
结果:
A journey of,a thousand 'miles' must can'<span style="color: #000000;">t "begin" with a single step.
result:
a journeY oF,A thousanD 'mileS' musT can'T "begiN" witH A singlE steP.<span style="color: #000000;">
source:<span style="color: #000000;">
A journey of,a thousand 'miles' must can'<span style="color: #000000;">t "begin" with a single step. a
result:
a journeY oF,A thousanD 'mileS' musT can'T "begiN" witH A singlE steP.<span style="color: #000000;"> A
source:<span style="color: #000000;">
A journey of,a thousand 'miles' must can'<span style="color: #000000;">t "begin" with a single step. a B
result:
a journeY oF,A thousanD 'mileS' musT can'T "begiN" witH A singlE steP.<span style="color: #000000;"> A b
source:<span style="color: #000000;">
A journey of,a thousand 'miles' must can't "begin" with a single step. a b'<span style="color: #000000;">
result:<span style="color: #000000;">
a journeY oF,A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A B'<span style="color: #000000;">
source:<span style="color: #000000;">
A journey of,a thousand 'miles' must can'<span style="color: #000000;">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"<span style="color: #000000;">
我们可以看到,是符合预期的。