|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +* EJERCICIO: |
| 5 | +* Muestra ejemplos de todas las operaciones que puedes realizar con cadenas de caracteres |
| 6 | +* en tu lenguaje. Algunas de esas operaciones podrían ser (busca todas las que puedas): |
| 7 | +* - Acceso a caracteres específicos, subcadenas, longitud, concatenación, repetición, recorrido, |
| 8 | +* conversión a mayúsculas y minúsculas, reemplazo, división, unión, interpolación, verificación... |
| 9 | +* |
| 10 | +* DIFICULTAD EXTRA (opcional): |
| 11 | +* Crea un programa que analice dos palabras diferentes y realice comprobaciones |
| 12 | +* para descubrir si son: |
| 13 | +* - Palíndromos |
| 14 | +* - Anagramas |
| 15 | +* - Isogramas |
| 16 | +*/ |
| 17 | + |
| 18 | +$string = "String"; |
| 19 | +$string_2 = " String 2"; |
| 20 | +$string_3 = "Another"; |
| 21 | + |
| 22 | +// Returns the length of the string |
| 23 | +echo "The length of string is ".strlen($string).".\n"; |
| 24 | + |
| 25 | +// Makes a string to lowercase |
| 26 | +echo strtolower($string).".\n"; |
| 27 | + |
| 28 | +// Makes a string to uppercase |
| 29 | +echo strtoupper($string).".\n"; |
| 30 | + |
| 31 | +// Return a part of the string |
| 32 | +echo substr($string,0,3).".\n"; |
| 33 | + |
| 34 | +// Replace all occurrences of the search string with the replacement string |
| 35 | +echo str_replace("String", "Hola", $string).".\n"; |
| 36 | + |
| 37 | +// Returns a string with the first character of str lowercase if that character is alphabetic. |
| 38 | +echo lcfirst($string)."\n"; |
| 39 | + |
| 40 | +// Removes whitespace (or other characters) from the beginning of a string. |
| 41 | +echo trim($string_2)."\n"; |
| 42 | + |
| 43 | +// Returns the reversed string |
| 44 | +echo strrev($string)."\n"; |
| 45 | + |
| 46 | +class Funcionality |
| 47 | +{ |
| 48 | + public function palindromes(string $word_1, string $word_2): string |
| 49 | + { |
| 50 | + $word_1 = trim($word_1); |
| 51 | + $word_2 = trim($word_2); |
| 52 | + |
| 53 | + $convert_word = $this->toLowerAndReversed($word_1); |
| 54 | + $convert_word_2 = $this->toLowerAndReversed($word_2); |
| 55 | + |
| 56 | + if($convert_word === $word_1) { |
| 57 | + echo "The First Word is a Palindrome.\n"; |
| 58 | + } |
| 59 | + |
| 60 | + if($convert_word_2 === $word_2) { |
| 61 | + echo "The Second Word is a Palindrome.\n"; |
| 62 | + } |
| 63 | + |
| 64 | + return "The words ain't palindromes.\n"; |
| 65 | + } |
| 66 | + |
| 67 | + public function anagrams(string $word_1, string $word_2): string |
| 68 | + { |
| 69 | + $word_1 = strtolower(trim($word_1)); |
| 70 | + $word_2 = strtolower(trim($word_2)); |
| 71 | + |
| 72 | + $sorted_word_1 = $this->sortString($word_1); |
| 73 | + $sorted_word_2 = $this->sortString($word_2); |
| 74 | + |
| 75 | + if ($sorted_word_1 === $sorted_word_2) { |
| 76 | + return "The words are anagrams.\n"; |
| 77 | + } else { |
| 78 | + return "The words are not anagrams.\n"; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public function isograms(string $word_1, string $word_2) |
| 83 | + { |
| 84 | + $word_1 = strtolower(trim($word_1)); |
| 85 | + $word_2 = strtolower(trim($word_2)); |
| 86 | + |
| 87 | + $letters = str_split($word_1); |
| 88 | + $letters_2 = str_split($word_2); |
| 89 | + |
| 90 | + $is_isogram = $this->hasRepeatingLetters($letters); |
| 91 | + $is_isogram_2 = $this->hasRepeatingLetters($letters); |
| 92 | + |
| 93 | + if($is_isogram === true) { |
| 94 | + echo "The First Word is a Isogram.\n"; |
| 95 | + } |
| 96 | + |
| 97 | + if($is_isogram_2 === true) { |
| 98 | + echo "The Second Word is a Isogram.\n"; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public function hasRepeatingLetters(array $letters): bool |
| 103 | + { |
| 104 | + foreach ($letters as $letter => $count) { |
| 105 | + if ($count > 1) { |
| 106 | + return true; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + private function sortString(string $word): string |
| 114 | + { |
| 115 | + $letters = str_split($word); |
| 116 | + sort($letters); |
| 117 | + return implode('', $letters); |
| 118 | + } |
| 119 | + |
| 120 | + public function toLowerAndReversed(string $word): string |
| 121 | + { |
| 122 | + $lower_word = strtolower($word); |
| 123 | + return $reversed_word = strrev($lower_word); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +$instance = new Funcionality(); |
| 128 | + |
| 129 | +$instance->isograms($string,$string_3); |
| 130 | + |
| 131 | +echo "This is a program where you write two words and then it will analyze if the words are palindromes, anagrams or isograms.\n"; |
| 132 | + |
| 133 | +echo "Write the first word: "; |
| 134 | +$word_1 = fgets(STDIN); |
| 135 | + |
| 136 | +echo "Write the second word: "; |
| 137 | +$word_2 = fgets(STDIN); |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +$instance->palindromes($word_1, $word_2); |
| 142 | +$instance->anagrams($word_1, $word_2); |
| 143 | +$instance->isograms($word_1, $word_2); |
| 144 | + |
| 145 | + |
| 146 | + |
0 commit comments