|
| 1 | +using System; |
| 2 | + |
| 3 | +class Program |
| 4 | +{ |
| 5 | + static void esPalindromo(string str) |
| 6 | + { |
| 7 | + char[] reverse = str.ToCharArray(); |
| 8 | + Array.Reverse(reverse); |
| 9 | + |
| 10 | + string reversedString = new string(reverse); |
| 11 | + |
| 12 | + if (str == reversedString) |
| 13 | + { |
| 14 | + Console.WriteLine("La palabra es Palíndromo"); |
| 15 | + } |
| 16 | + else |
| 17 | + { |
| 18 | + Console.WriteLine("La palabra no es Palíndromo"); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + static bool esAnagrama(string str1, string str2) |
| 23 | + { |
| 24 | + // Verificar si ambas cadenas tienen la misma longitud |
| 25 | + if (str1.Length != str2.Length) |
| 26 | + { |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + // Convertir las cadenas a arreglos de caracteres y ordenarlos |
| 31 | + char[] arr1 = str1.ToCharArray(); |
| 32 | + char[] arr2 = str2.ToCharArray(); |
| 33 | + |
| 34 | + Array.Sort(arr1); |
| 35 | + Array.Sort(arr2); |
| 36 | + |
| 37 | + // Comparar los arreglos ordenados |
| 38 | + for (int i = 0; i < arr1.Length; i++) |
| 39 | + { |
| 40 | + if (arr1[i] != arr2[i]) |
| 41 | + { |
| 42 | + return false; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Si todos los caracteres son iguales, son anagramas |
| 47 | + return true; |
| 48 | + } |
| 49 | + |
| 50 | + static bool esIsograma(string str) |
| 51 | + { |
| 52 | + // Convertir la cadena a un array de caracteres |
| 53 | + char[] caracteres = str.ToCharArray(); |
| 54 | + |
| 55 | + // Ordenar el array para facilitar la comparación |
| 56 | + Array.Sort(caracteres); |
| 57 | + |
| 58 | + // Verificar si hay caracteres repetidos |
| 59 | + for (int i = 0; i < caracteres.Length - 1; i++) |
| 60 | + { |
| 61 | + if (caracteres[i] == caracteres[i + 1]) |
| 62 | + { |
| 63 | + return false; // Hay caracteres repetidos |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Si no hay caracteres repetidos, es un isograma |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + static void Main() |
| 72 | + { |
| 73 | + // Crear una cadena |
| 74 | + string cadena1 = "Hola, "; |
| 75 | + string cadena2 = "mundo!"; |
| 76 | + |
| 77 | + // Acceso a caracteres específicos |
| 78 | + char primerCaracter = cadena1[0]; |
| 79 | + char ultimoCaracter = cadena2[cadena2.Length - 1]; |
| 80 | + |
| 81 | + Console.WriteLine("Primer carácter: " + primerCaracter); |
| 82 | + Console.WriteLine("Último carácter: " + ultimoCaracter); |
| 83 | + |
| 84 | + // Longitud de la cadena |
| 85 | + int longitud = cadena1.Length; |
| 86 | + |
| 87 | + Console.WriteLine("Longitud de la cadena: " + longitud); |
| 88 | + |
| 89 | + // Concatenación |
| 90 | + string concatenacion = cadena1 + cadena2; |
| 91 | + |
| 92 | + Console.WriteLine("Concatenación: " + concatenacion); |
| 93 | + |
| 94 | + // Repetición |
| 95 | + string repeticion = new string('!', 3); |
| 96 | + |
| 97 | + Console.WriteLine("Repetición: " + repeticion); |
| 98 | + |
| 99 | + // Recorrido |
| 100 | + foreach (char caracter in concatenacion) |
| 101 | + { |
| 102 | + Console.WriteLine(caracter); |
| 103 | + } |
| 104 | + |
| 105 | + // Conversión a mayúsculas y minúsculas |
| 106 | + string mayusculas = concatenacion.ToUpper(); |
| 107 | + string minusculas = concatenacion.ToLower(); |
| 108 | + |
| 109 | + Console.WriteLine("Mayúsculas: " + mayusculas); |
| 110 | + Console.WriteLine("Minúsculas: " + minusculas); |
| 111 | + |
| 112 | + // Reemplazo |
| 113 | + string reemplazo = concatenacion.Replace("mundo", "amigo"); |
| 114 | + |
| 115 | + Console.WriteLine("Reemplazo: " + reemplazo); |
| 116 | + |
| 117 | + // División |
| 118 | + string[] partes = concatenacion.Split(','); |
| 119 | + |
| 120 | + foreach (string parte in partes) |
| 121 | + { |
| 122 | + Console.WriteLine("Parte: " + parte.Trim()); |
| 123 | + } |
| 124 | + |
| 125 | + // Unión |
| 126 | + string[] palabras = { "Hola", "cómo", "estás" }; |
| 127 | + string union = string.Join(" ", palabras); |
| 128 | + |
| 129 | + Console.WriteLine("Unión: " + union); |
| 130 | + |
| 131 | + // Interpolación |
| 132 | + string nombre = "RX"; |
| 133 | + int edad = 30; |
| 134 | + |
| 135 | + string mensaje = $"Hola, me llamo {nombre} y tengo {edad} años."; |
| 136 | + |
| 137 | + Console.WriteLine(mensaje); |
| 138 | + |
| 139 | + // Verificación |
| 140 | + bool contieneMundo = concatenacion.Contains("mundo"); |
| 141 | + |
| 142 | + Console.WriteLine("Contiene 'mundo': " + contieneMundo); |
| 143 | + |
| 144 | + // Comparación de cadenas |
| 145 | + string str1 = "abc"; |
| 146 | + string str2 = "def"; |
| 147 | + |
| 148 | + bool sonIguales = String.Equals(str1, str2); |
| 149 | + int comparacion = String.Compare(str1, str2); |
| 150 | + |
| 151 | + // Búsqueda y posición |
| 152 | + int indice = cadena1.IndexOf("la"); |
| 153 | + int ultimoIndice = cadena1.LastIndexOf("la"); |
| 154 | + |
| 155 | + // Eliminación y truncamiento |
| 156 | + string nuevaCadena = cadena1.Remove(2, 3); |
| 157 | + string truncada = cadena1.Substring(0, 4); |
| 158 | + |
| 159 | + // Formato |
| 160 | + string formato = String.Format("La temperatura es {0} grados Celsius.", 25); |
| 161 | + |
| 162 | + // Caracteres especiales |
| 163 | + string nuevaLinea = "Primera línea\nSegunda línea"; |
| 164 | + string tabulacion = "Columna1\tColumna2"; |
| 165 | + |
| 166 | + // Espacios en blanco |
| 167 | + string conEspacios = " Hola "; |
| 168 | + string sinEspacios = conEspacios.Trim(); |
| 169 | + |
| 170 | + //Dificultad extra: |
| 171 | + string s1 = "asdsaa"; |
| 172 | + string s2 = "aasdsa"; |
| 173 | + Console.WriteLine(); |
| 174 | + esPalindromo(s1); |
| 175 | + Console.WriteLine(); |
| 176 | + |
| 177 | + if (esAnagrama(s1, s2)) |
| 178 | + { |
| 179 | + Console.WriteLine("La palabra es Anagrama"); |
| 180 | + }else { Console.WriteLine("La palabra no es Anagrama"); } |
| 181 | + Console.WriteLine(); |
| 182 | + |
| 183 | + if (esIsograma(s1)) |
| 184 | + { |
| 185 | + Console.WriteLine("La palabra es Isograma"); |
| 186 | + } |
| 187 | + else { Console.WriteLine("La palabra no es Isograma"); } |
| 188 | + |
| 189 | + Console.ReadKey(); |
| 190 | + } |
| 191 | +} |
0 commit comments