Requirements
Given a string such as "A journey of, a thousand ' miles ' must can ' t \" begin\ "with a single step." By PHP program processing becomes "A journey of, a thous and ' MileS ' must can ' T ' BegiN ' with A single SteP. "
Attention:
1. If the last character of each word is uppercase, it becomes lowercase, and if lowercase, it becomes uppercase.
2. It is necessary to consider the conversion of this form like can ' t.
3, punctuation (only consider, ' ".; ) without change.
Reference algorithms
<?PHPfunctionConvertlastchar ($str) { $MARKARR=Array(", ", "‘ ", "\" ", ". ", "; "); $ret= ""; for($i= 0,$j=strlen($str);$i<$j;$i++) { if($i<$j+ t) { $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; }?>
Test
<?PHP//Test $str 1= "A journey of, a thousand ' miles ' must can ' t \" begin\ "with A single step."; $str 2= "A journey of, a thousand ' miles ' must can ' t \" begin\ "with A single step. "; $STR 3= "A journey of, a thousand ' miles ' must can ' t \" begin\ "with A single step. A; $STR 4= "A journey of, a thousand ' miles ' must can ' t \" begin\ "with A single step. A B "; $STR 5= "A journey of, a thousand ' miles ' must can ' t \" begin\ "with A single step. A B ' "; $str 6= "A journey of, a thousand ' miles ' must can ' t \" begin\ "with A single step. A b\ ""; Echo"Source:<br/>".$str 1. "<br/>result:<br/>". Convertlastchar ($str 1) . "<br/><br/>"; Echo"Source:<br/>".$str 2. "<br/>result:<br/>". Convertlastchar ($str 2) . "<br/><br/>"; Echo"Source:<br/>".$STR 3. "<br/>result:<br/>". Convertlastchar ($STR 3) . "<br/><br/>"; Echo"Source:<br/>".$STR 4. "<br/>result:<br/>". Convertlastchar ($STR 4) . "<br/><br/>"; Echo"Source:<br/>".$STR 5. "<br/>result:<br/>". Convertlastchar ($STR 5) . "<br/><br/>"; Echo"Source:<br/>".$str 6. "<br/>result:<br/>". Convertlastchar ($str 6) . "<br/><br/>";?>
Results:
SourceA 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 bresult:a JourneY of, a thousand' MileS ' must can ' T ' BegiN ' with A single SteP.A Bsource: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 "
We can see that it is in line with expectations.
Source of the topic
Http://blog.sijiaomao.com/?p=98, there are changes (increase can ' t this), according to the revised rules, the original answer is all wrong.
PHP string Word end character Case interchange