-
Notifications
You must be signed in to change notification settings - Fork 0
/
ejercicio7_ParseStrings.java
82 lines (71 loc) · 2.52 KB
/
ejercicio7_ParseStrings.java
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
81
82
import java.util.Scanner;
/**
* Main class of the Java program.
*
* Alumno: Pablo Gonzalez Troyano
* Grado en Ciencia, Gestion e Ingenieria de Servicios (Semipresencial)
*
*/
public class ParseStrings {
public static void main(String[] args) {
int condicion = 0;
System.out.println("Enter input: ");
String input = "";
Scanner scan = new Scanner(System.in);
input = scan.nextLine();
String parsing_info = parseString(input);
String input_reversed = reverseString(input);
int num_vowels = checkVowels(input);
int num_digits = checkDigits(input);
System.out.println(parsing_info);
System.out.println("The input is: " + input);
System.out.println("The reverse of the string '" + input + "' is " + input_reversed);
System.out.println("The input has " + num_vowels + " vowels");
System.out.println("The input has " + num_digits + " digits");
}
public static String parseString (String input) {
String output = "The input has ";
if (input.length() < 5){
output = output + "less than 5 characters and ";
} else if(input.length() > 15)
output = output + "more than 15 characters and ";
else {
output = output + "between 5 and 15 characters and ";
};
if (input.charAt(0) == 'a'){
output = output + "begins with an 'a'.";
} else {
output = output + "does not begin with an 'a'";
};
return output;
}
public static String reverseString (String input) {
String output ="";
for (int i = 0; i < input.length(); i++) {
output = input.charAt(i) + output;
}
return output;
}
public static int checkVowels (String input) {
int count = 0; char letra;
input = input.toLowerCase();
for (int i = 0; i < input.length(); i++) {
letra = input.charAt(i);
if (letra == 'a' || letra == 'e' || letra == 'i' || letra == 'o' || letra == 'u'){
count++;
};
};
return count;
};
public static int checkDigits (String input) {
int count = 0;
// Vease: https://docs.oracle.com/javase/6/docs/api/java/lang/Character.html#isDigit(char)
for (int i = 0; i < input.length(); i++) {
char letra = input.charAt(i);
if(Character.isDigit(letra)){
count++;
};
}
return count;
}
}