-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuscaPalabra.java
More file actions
75 lines (49 loc) · 1.76 KB
/
BuscaPalabra.java
File metadata and controls
75 lines (49 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.Scanner;
public class BuscaPalabra {
public static int buscarCaracter(String texto, char c){
int contador = 0;
for(int i=0; i<texto.length(); i++){
if(texto.charAt(i)==c);{
contador++;
}
}
return 0;
}
public static void pintarDoble(int numero){
System.out.println(2*numero);
}
public static char buscarLetra(String texto, int posicion){
if(posicion>=texto.length()){
return ' ';
}else{
return texto.charAt(posicion);
}
}
public static String reemplazarPalabra(String textOriginal, String textoBuscar, String nuevoTexto) {
if (textOriginal.contains(textoBuscar)) {
return textOriginal.replace(textoBuscar, nuevoTexto);
}else{
return "texto no encontrado";
}
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("introduce texto: ");
String texto = scanner.nextLine();
System.out.println("introduce caracter: ");
char caracter = scanner.nextLine().charAt(0);
System.out.println("texto a buscar: ");
int total = buscarCaracter(texto, caracter);
System.out.printf("aparece %d veces", total);
String textoRemplazar = scanner.nextLine();
System.out.println("nuevo texto: ");
String textoNuevo = scanner.nextLine();
String textoReemplazado = (reemplazarPalabra(texto, textoRemplazar, textoNuevo));
System.out.printf("texto reemplazado: %s%n", textoReemplazado);
System.out.println("introduce la posicion: ");
int posicion = scanner.nextInt();
char letra = buscarLetra(textoReemplazado, posicion);
System.out.println(letra);
scanner.close();
}
}