Java - RECURSIVIDAD CON VOCALES

 
Vista:

RECURSIVIDAD CON VOCALES

Publicado por Pris (4 intervenciones) el 06/08/2017 20:47:19
TENGO QUE HACER UN PROGRAMA DONDE EL USUARIO INGRESE UN TEXTO Y EL SISTEMA LE DIGA LA CANTIDAD DE VOCALES Y CUALES SON DE FORMA RECURSIVA. TENGO ESTA LINEA DE CODIGO Y ME LOGRA DECIR CUANTAS VOCALES HAY PERO NO CUALES SON, ALGUIEN PODRIA AYUDARME POR FAVOR

CODIGO:

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
public static int vocales(String cad) {
	if (cad.length() == 0) {
		return 0;
	}
	switch (Character.toLowerCase(cad.charAt(cad.length() - 1))) {
		case 'a':
			return 1 + vocales(cad.substring(0, cad.length() - 1));
		case 'e':
			return 1 + vocales(cad.substring(0, cad.length() - 1));
		case 'i':
			return 1 + vocales(cad.substring(0, cad.length() - 1));
		case 'o':
			return 1 + vocales(cad.substring(0, cad.length() - 1));
		case 'u':
			return 1 + vocales(cad.substring(0, cad.length() - 1));
	}
	return 0 + vocales(cad.substring(0, cad.length() - 1));
}
 
 
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.print("Ingrese el texto");
String cadena = sc.nextLine();
System.out.println("vocales:" + vocales(cadena);
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
1
Responder
sin imagen de perfil
Val: 349
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

RECURSIVIDAD CON VOCALES

Publicado por Andrés (340 intervenciones) el 07/08/2017 00:15:55
Te hace falta un acumulador,

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
76
77
78
79
80
public class Vowels {
 
	public void countVowels(final String text, final int count[], int index) {
 
		if(null == text || text.trim().isEmpty()) {
 
			throw new IllegalArgumentException("Error... ");
 
		}
 
		if(index > text.length()) {
 
			throw new IllegalArgumentException("Error... ");
 
		}
 
		//We reach the end
		if(index == text.length()) {
			//We are done!
			return;
 
		}else {
 
			int difference = -1;
			final int c = text.charAt(index++);
 
			switch(c) {
 
				case 97:
					difference = 97;
					break;
				case 101:
					difference = 100;
					break;
				case 105:
					difference = 103;
					break;
				case 111:
					difference = 108;
					break;
				case 117:
					difference = 113;
					break;
 
			}
 
			if(-1 == difference) {
                                 //It was not a vowel
				countVowels(text, count, index);
 
			}else {
				count[c-difference]++;
				countVowels(text, count, index);
			}
 
 
		}
 
 
	}
 
	public static void main(String...strings) {
 
		int index = 0;
		final String text = "u uoi ii iee";
		final int count[] = new int[5];
 
		Vowels vowels = new Vowels();
		vowels.countVowels(text, count, index);
 
		System.out.println(String.format("%1s: %3d","a",count[0]));
		System.out.println(String.format("%1s: %3d","e",count[1]));
		System.out.println(String.format("%1s: %3d","i",count[2]));
		System.out.println(String.format("%1s: %3d","o",count[3]));
		System.out.println(String.format("%1s: %3d","u",count[4]));
 
 
	}
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RECURSIVIDAD CON VOCALES

Publicado por Costero (148 intervenciones) el 07/08/2017 03:05:59
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
public class RecursiveCounter {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Ingrese el texto");
        String cadena = sc.nextLine();
        System.out.println("vocales:" + countLetters(cadena));
    }
 
 
    private static String countLetters(String phrase) {
 
        Map<Character,Integer> counter = new HashMap<>();
        recursionCounter(phrase, 0, counter);
        return counter.toString();
    }
 
 
    private static void recursionCounter(String word, int index, Map<Character,Integer> counter) {
 
        if(index < word.length()) {
            char value = word.charAt(index);
 
            if(counter.containsKey(value)) {
                counter.put(value, counter.get(value) + 1);
            } else {
                counter.put(value, 1);
            }
            recursionCounter(word, ++index, counter);
        }
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RECURSIVIDAD CON VOCALES

Publicado por karlx (1 intervención) el 28/09/2017 21:49:24
le falta un parentesis al final )
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar