String_8-Comprobar+si+una+frase+es+un+palíndromo

=__**Leer una frase y comprobar si es palíndromo**__=

code format="pascal" PROGRAM P_63_E_8 ; { strin_8 } USES Crt ; CONST iLONG_FISICA = 79 ; TYPE tsCadena = String [iLONG_FISICA] ; VAR sCadena : tsCadena ; cEspera : Char ;

FUNCTION fnsPasarAMayusculas (sCadena : tsCadena) : tsCadena ; VAR i : Byte ; BEGIN FOR i := 1 TO Length (scadena) DO       CASE sCadena[i] OF               'á' : sCadena[i] := 'A' ; 'é' : sCadena[i] := 'E' ; 'í' : sCadena[i] := 'I' ; 'ó' : sCadena[i] := 'O' ; 'ú' : sCadena[i] := 'U' ; 'ñ' : sCadena[i] := 'Ñ' ; 'a'..'z' : sCadena[i] := Upcase (sCadena[i]) END ;

fnsPasarAMayusculas := sCadena END ; { fnsPasarAMayusculas }

{--- Si no funciona la anterior versión - FUNCTION fnsPasarAMayusculas (sCadena : tsCadena) : tsCadena ; VAR i : Byte ; BEGIN FOR i := 1 TO Length (sCadena) DO       CASE sCadena[i] OF               #160 : sCadena[i] := 'A' ;    { 'á' -> 'A' } #130 : sCadena[i] := 'E' ;   { 'é' -> 'A' } #161 : sCadena[i] := 'I' ;   { 'í' -> 'A' } #162 : sCadena[i] := 'O' ;   { 'ó' -> 'A' } #163 : sCadena[i] := 'U' ;   { 'ú' -> 'A' } #164 : sCadena[i] := #165    { 'ñ' -> 'Ñ' } ; 'a'..'z' : sCadena[i] := Upcase (sCadena[i]) END ;

fnsPasarAMayusculas := sCadena END ; { fnsPasarAMayusculas } --}

FUNCTION fnsDejarSoloLetras (sCadena : tsCadena) : tsCadena ; VAR i : Byte ; BEGIN i := 1 ;

WHILE i <= Length (scadena) DO       IF ('A' <= sCadena[i]) AND (sCadena[i] <= 'Z') THEN i := i + 1 ELSE Delete (sCadena, i, 1) ;

fnsDejarSoloLetras := sCadena END ; { fnsDejarSoloLetras }

FUNCTION fnboEsPalindromo (sCadena : tsCadena) : Boolean ; VAR i, byLongitudCadena, byComparaciones : Byte ; boPalindromo : Boolean ; BEGIN boPalindromo := TRUE ;

byLongitudCadena := Length (sCadena) ; byComparaciones := byLongitudCadena DIV 2 ;

i := 1 ;

WHILE boPalindromo AND (i <= byComparaciones) DO      IF sCadena [i] <> sCadena [byLongitudCadena - i + 1]  THEN boPalindromo := FALSE ELSE i := i + 1 ;

fnboEsPalindromo := boPalindromo END ; { fnboEsPalindromo }

BEGIN {***************** Programa Principal *******************} Writeln ; Writeln ; WriteLn ('Programa que indica si una frase es un pal¡ndromo o no') ; Writeln ; Writeln ('Ejemplos: acaso hubo buhos acá ') ; Writeln ('         Yo hago yoga hoy') ; Writeln ('         Sé verlas al revés') ; Writeln ; Write ('Escriba una frase: ') ; Readln   (sCadena) ; Writeln ; writeln ;

{ Pasar los caracteres alfab‚ticos a mayusculas } sCadena := fnsPasarAMayusculas (sCadena) ;

{ Eliminar todos los caracteres no alfab‚ticos } sCadena := fnsDejarSoloLetras (sCadena) ;

IF fnboEsPalindromo (sCadena) THEN Writeln (' ES un pal¡ndromo') ELSE Writeln (' NO ES un pal¡ndromo') ;

cEspera := Readkey ; END. { Fin de STRIN_7.PAS }

code