首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 字串單字尾字母大小寫互換演算法實作

     編輯:PHP 綜合知識

給定英文句子,將每個單字的最後一個字母大小寫互換(大變小、小變大),標點符號(, ' " . ;)不變,縮寫形式(如 can't)視為單一單字處理。本文提供完整演算法實作與測試案例。

一、需求分析

  • 輸入:"A journey of, a thousand 'miles' must can't \"begin\" with a single step."
  • 輸出:"a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP."
  • 規則:
    1. 單字最後一個字母大小寫互換
    2. can't 視為一個單字,尾字母 t 互換
    3. 標點 , ' " . ; 後若有空格則視為單字結尾

二、核心演算法:向前看兩字元判斷邊界

<?php
function convertLastChar($str) {
    // 標點+空格組合,標記單字結尾
    $markArr = [", ", "' ", "\" ", ". ", "; "];
    $ret = "";
    $len = strlen($str);

    for ($i = 0; $i < $len; $i++) {
        // 取得後續兩字元(含邊界保護)
        if ($i < $len - 2) {
            $afterStr = $str[$i + 1] . $str[$i + 2];
        } elseif ($i < $len - 1) {
            $afterStr = $str[$i + 1] . " ";
        } else {
            $afterStr = "  "; // 字串結尾
        }

        // 判斷目前字元是否為單字尾字母
        $isLastChar = in_array($afterStr, $markArr) 
                   || $i == $len - 1 
                   || $str[$i + 1] == " ";

        if ($isLastChar) {
            // 大小寫互換
            $char = $str[$i];
            $ret .= (strtoupper($char) === $char) 
                  ? strtolower($char) 
                  : strtoupper($char);
        } else {
            $ret .= $str[$i];
        }
    }
    return $ret;
}
?>

三、測試案例與輸出

<?php
$tests = [
    "A journey of, a thousand 'miles' must can't \"begin\" with a single step.",
    "A journey of, a thousand 'miles' must can't \"begin\" with a single step. ",
    "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a ",
    "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a B",
    "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a b'",
    "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a B\""
];

foreach ($tests as $idx => $str) {
    echo "Test " . ($idx+1) . ":\n";
    echo "Source: $str\n";
    echo "Result: " . convertLastChar($str) . "\n\n";
}
?>

輸出結果

Test 1:
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.

Test 2:
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.

Test 3:
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

Test 4:
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

Test 5:
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'

Test 6:
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"

四、演算法關鍵點解析

  1. 向前看策略:檢查 $i+1 和 $i+2 位置,識別「標點+空格」組合作為單字邊界
  2. 邊界處理:字串末尾、單一空格分隔皆正確判定為單字結尾
  3. 縮寫支援:can't 中 ' 後無空格,不觸發邊界判斷,t 正確被識別為尾字母
  4. 大小寫判斷:用 strtoupper($char) === $char 避免 ctype_upper() 受 locale 影響

五、擴充建議

  • 支援 Unicode 字母(需 mb_strlen/mb_substr + mb_strtoupper)
  • 可配置標點集合(目前固定 5 種)
  • 效能優化:預編譯標點集合為查找表

此演算法展示字串逐字元掃描配合向前看策略,解決自然語言邊界識別的典型技巧。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved