-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEx1nX.java
53 lines (44 loc) · 1.62 KB
/
Ex1nX.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
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Objects;
public class Ex1nX
{
public static void main (String[]args)
{
Scanner input = new Scanner (System.in);
int location, index;
char letter;
int[] letterCount = new int[26]; // vetor do tipo int para contagem das letras
System.out.println ("Digite uma string:");
String line = input.nextLine (); // string de leitura da linha
while (line != null) // loop de enquando houver linhas a serem lidas
{
for (location = 0; location < line.length (); location++) // loop para a contagem das letras
{
letter = line.charAt (location);
if ((letter >= 'A' && letter <= 'Z') || (letter >= 'a' && letter <= 'z'))
{
// 2o truque
index = (int) Character.toUpperCase (letter) - (int) 'A';
letterCount[index]++;
}
}
for (index = 0; index < letterCount.length; index++) // loop para imprimir a contagem das letras
{
if(letterCount[index]>0) // somente imprime o numero total de letras caso seja maior que zero
{
System.out.println ("O numero total de " +
(char) (index + (int) 'A') + "'s é " +
letterCount[index]);
}
}
System.out.println ("Digite uma string:"); // pede novamente outra string
line = input.nextLine ();
if (Objects.equals(line, "")) // caso o usuario tecle 'enter' sem digitar outros caracteres
{
line = null; // atribui 'null' a linha e assim encerra o loop
}
}
}
}