Palindromes
Checks whether a phrase reads the same forwards and backwards.
- Level
- Intermediate
- Topic:
- Text
- Concepts:
- Input & output
- Text
- Para
- Si
Open in Codoritmo
Run, change and debug this example in your browser.
What you'll learn
Subcadena pulls out one letter at a time and Minusculas normalizes it. A Para loop with Con Paso -1 walks the text backwards to build the reversed version.
Code
Algoritmo Palindromo Definir frase, limpia, invertida, letra Como Caracter Definir i Como Entero Escribir "Escribe una palabra o frase:" Leer frase // Quitamos los espacios y pasamos todo a minúsculas limpia <- "" Para i <- 1 Hasta Longitud(frase) Hacer letra <- Minusculas(Subcadena(frase, i, i)) Si letra <> " " Entonces limpia <- limpia + letra FinSi FinPara invertida <- "" Para i <- Longitud(limpia) Hasta 1 Con Paso -1 Hacer invertida <- invertida + Subcadena(limpia, i, i) FinPara Si limpia = invertida Entonces Escribir 'Sí, "', frase, '" es un palíndromo.' SiNo Escribir 'No es un palíndromo: al revés se lee "', invertida, '".' FinSiFinAlgoritmoFlowchart
Each block in the flowchart matches one instruction in the code.
Sample run
This is what the program shows when it runs. The answers typed by the user are highlighted.
Escribe una palabra o frase:User answer: Anita lava la tinaSí, "Anita lava la tina" es un palíndromo.
Challenge
Also ignore commas and periods, so "Yo hago yoga, hoy." counts as a palindrome.
Show hint
Extend the Si condition that skips the spaces.
Related examples
Name Initials
Gets the initials of a full name.
BeginnerCaesar Cipher
Encrypts a message by shifting each letter of the alphabet.
AdvancedStore Inventory
Records the stock of each product and flags what to restock.
Intermediate