|
| 1 | + |
| 2 | +import java.util.*; |
| 3 | +public class simonguzman { |
| 4 | + public static void main(String[] args) { |
| 5 | + |
| 6 | + String str1 = " Hello "; |
| 7 | + String str2 = " Challenge # 03 "; |
| 8 | + String str3 = " character "; |
| 9 | + String str4 = " strings "; |
| 10 | + |
| 11 | + //Concatenar |
| 12 | + String concatenatedString = str1 + str2 + str3 + str4; |
| 13 | + System.out.println(concatenatedString); |
| 14 | + String concatenatedString2 = str1.concat(concatenatedString); |
| 15 | + System.out.println(concatenatedString2); |
| 16 | + |
| 17 | + //Obtener caracter en posicion especifica |
| 18 | + char ch = str1.charAt(2); |
| 19 | + System.out.println(ch); |
| 20 | + |
| 21 | + //Subcadena de una cadena |
| 22 | + String sub = str1.substring(2); |
| 23 | + System.out.println(sub); |
| 24 | + String sub2 = str1.substring(2, 4); |
| 25 | + System.out.println(sub2); |
| 26 | + |
| 27 | + //Longitud de una cadena |
| 28 | + int len = str1.length(); |
| 29 | + System.out.println(len); |
| 30 | + |
| 31 | + //comparando 2 cadenas(sensible a las mayusculas) |
| 32 | + boolean isEqual = str1.equals(" HELLO "); |
| 33 | + System.out.println(isEqual); |
| 34 | + //Comparando 2 cadenas(sin importar las mayusculas) |
| 35 | + boolean isEqual2 = str1.equalsIgnoreCase(" HELLO "); |
| 36 | + System.out.println(isEqual2); |
| 37 | + |
| 38 | + //Comparacion alfabetica de 2 cadenas(sensible a mayusculas) |
| 39 | + int compare = str1.compareTo(" hello "); |
| 40 | + System.out.println(compare); |
| 41 | + //Comparacion alfabetica de 2 cadenas(sin importar las mayusculas) |
| 42 | + int compare2 = str1.compareToIgnoreCase(" hello "); |
| 43 | + System.out.println(compare2); |
| 44 | + |
| 45 | + //Repetir una cadena |
| 46 | + String repeatString = str1.repeat(3); |
| 47 | + System.out.println(repeatString); |
| 48 | + |
| 49 | + //Reemplazar caracteres |
| 50 | + String charReplaced = str1.replace("l", "d"); |
| 51 | + System.out.println(charReplaced); |
| 52 | + //Reemplazar subcadenas |
| 53 | + String subStrReplaced = str1.replace("ell", "all"); |
| 54 | + System.out.println(subStrReplaced); |
| 55 | + |
| 56 | + //Convertir a mayusculas |
| 57 | + String upper = str1.toUpperCase(); |
| 58 | + System.out.println(upper); |
| 59 | + //Convertir a minusculas |
| 60 | + String lower = str1.toLowerCase(); |
| 61 | + System.out.println(lower); |
| 62 | + |
| 63 | + //Eliminar espacios en blanco al inicio y al final |
| 64 | + String trimmed = str1.trim(); |
| 65 | + System.out.println(trimmed); |
| 66 | + |
| 67 | + //Convertir a otros tipos de cadena |
| 68 | + String fromInt = str1.valueOf(123); |
| 69 | + System.out.println(fromInt); |
| 70 | + String fromChar = str1.valueOf('a'); |
| 71 | + System.out.println(fromChar); |
| 72 | + |
| 73 | + //Si una cadena esta dentro de otra |
| 74 | + boolean contains = str1.contains("ell"); |
| 75 | + System.out.println(contains); |
| 76 | + //Verificar si comienza con una subcadena |
| 77 | + boolean startsWith = str1.startsWith(" Hel"); |
| 78 | + System.out.println(startsWith); |
| 79 | + //Verificar si termina con una subcadena |
| 80 | + boolean endsWith = str1.endsWith("llo "); |
| 81 | + System.out.println(endsWith); |
| 82 | + |
| 83 | + //Buscar posicion de una caracter o subcadena |
| 84 | + int index = str1.indexOf('l'); |
| 85 | + System.out.println(index); |
| 86 | + int lastIndex = str1.lastIndexOf("l"); |
| 87 | + System.out.println(lastIndex); |
| 88 | + int subIndex = str1.indexOf("ell"); |
| 89 | + System.out.println(subIndex); |
| 90 | + |
| 91 | + //Verificar si una cadena esta vacia |
| 92 | + boolean isEmpty = str1.isEmpty(); |
| 93 | + System.out.println(isEmpty); |
| 94 | + |
| 95 | + //Si una cadena es nula |
| 96 | + boolean isBlank = str1 == null; |
| 97 | + System.out.println(isBlank); |
| 98 | + |
| 99 | + //Recorrer cada caracter de la cadena |
| 100 | + for(int i = 0; i < str1.length(); i++){ |
| 101 | + System.out.println(str1.charAt(i)); |
| 102 | + } |
| 103 | + |
| 104 | + //Invertir una cadena |
| 105 | + String reversed = ""; |
| 106 | + for(int i = str1.length() - 1; i >= 0; i--){ |
| 107 | + reversed += str1.charAt(i); |
| 108 | + } |
| 109 | + System.out.println(str1); |
| 110 | + System.out.println(reversed); |
| 111 | + |
| 112 | + ingresarPalabras(); |
| 113 | + } |
| 114 | + |
| 115 | + //******************************** Ejercicio adicional ********************************/ |
| 116 | + |
| 117 | + public static void ingresarPalabras(){ |
| 118 | + Scanner scanner = new Scanner(System.in); |
| 119 | + String word1 = " "; |
| 120 | + String word2 = " "; |
| 121 | + System.out.println("Ingrese la primer palabra: "); |
| 122 | + word1 = scanner.next(); |
| 123 | + System.out.println("Ingrese la segunda palabra: "); |
| 124 | + word2 = scanner.next(); |
| 125 | + palindromo(word1, word2); |
| 126 | + anagrama(word1, word2); |
| 127 | + isograma(word1, word2); |
| 128 | + scanner.close(); |
| 129 | + } |
| 130 | + |
| 131 | + public static void anagrama(String word1, String word2) { |
| 132 | + String difOrderWord1 = ordenarPalabra(word1); |
| 133 | + String difOrderWord2 = ordenarPalabra(word2); |
| 134 | + |
| 135 | + if ( difOrderWord1.equals(difOrderWord2)){ |
| 136 | + System.out.println("Las palabras son anagramas"); |
| 137 | + }else{ |
| 138 | + System.out.println("Las palabras no son anagramas"); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public static void isograma(String word1, String word2){ |
| 143 | + boolean wordIsogram = verificarDuplicados(word1); |
| 144 | + boolean wordIsogram2 = verificarDuplicados(word2); |
| 145 | + if(wordIsogram){ |
| 146 | + System.out.println("La palabra es isograma"); |
| 147 | + }else{ |
| 148 | + System.out.println("La palabra no es isograma"); |
| 149 | + } |
| 150 | + |
| 151 | + if(wordIsogram2){ |
| 152 | + System.out.println("La palabra es isograma"); |
| 153 | + }else{ |
| 154 | + System.out.println("La palabra no es isograma"); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + public static void palindromo(String word1, String word2){ |
| 159 | + String reverseWord1 = InvertirPalabra(word1); |
| 160 | + String reverseWord2 = InvertirPalabra(word2); |
| 161 | + |
| 162 | + if(word1.equals(reverseWord1)){ |
| 163 | + System.out.println("Es palindromo"); |
| 164 | + }else{ |
| 165 | + System.out.println("No es palindromo"); |
| 166 | + } |
| 167 | + if(word2.equals(reverseWord2)){ |
| 168 | + System.out.println("Es palindromo"); |
| 169 | + }else{ |
| 170 | + System.out.println("No es palindromo"); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + public static String InvertirPalabra(String word){ |
| 175 | + String reverseWord = ""; |
| 176 | + for (int i = word.length() - 1; i >= 0; i--){ |
| 177 | + reverseWord += word.charAt(i); |
| 178 | + } |
| 179 | + return reverseWord; |
| 180 | + } |
| 181 | + |
| 182 | + public static String ordenarPalabra(String word){ |
| 183 | + char [] sortWork = word.toCharArray(); |
| 184 | + Arrays.sort(sortWork); |
| 185 | + |
| 186 | + String newWork = new String(sortWork); |
| 187 | + return newWork; |
| 188 | + } |
| 189 | + |
| 190 | + public static boolean verificarDuplicados(String word){ |
| 191 | + Set characthers = new HashSet<>(); |
| 192 | + for (int i = 0; i < word.length(); i++){ |
| 193 | + char c = word.charAt(i); |
| 194 | + if(characthers.contains(c)){ |
| 195 | + return false; |
| 196 | + } |
| 197 | + characthers.add(c); |
| 198 | + } |
| 199 | + return true; |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments