Ejemplo 1: contar el número de caracteres en una cadena java
public static void main(String[] args) {
int wordCount = 0;
String word = "hill";
for (char letter = 'a'; letter <= 'z'; letter++) {
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == letter) {
wordCount++;
}
}
if (wordCount > 0) {
System.out.println(letter + "=" + wordCount);
wordCount = 0;
}
}
}
Ejemplo 2: ¿Cómo se cuentan los caracteres en una matriz de cadenas en Java?
package practical5;
import java.util.Arrays;
public class Part1_9 {
public static void main(String[] args) {
// declaring and populating array
String quoteArray[] = { ""Continuous", "effort", "not", "strength",
"nor", "intelligence", "is", "the", "key", "to", "unlocking",
"our", "potential."n" };
// for loop to print full array
for (int counter = 0; counter < quoteArray.length; counter++) {
System.out.print(quoteArray[counter] + " ");
}// end of for loop
// Printing array using Enhanced for/ for each loop (Different way to
// print array)
for (String element : quoteArray) {
System.out.print(element + " ");
}// end of enhanced for
// line break
System.out.println();
// printing number of words in array
System.out.println("Number of words in array: " + quoteArray.length);
**// printing total number of letters in array**
for (int counter = 0; counter < quoteArray.length; counter++) {
String letters = new String(quoteArray[counter]);
}
// printing the smallest word
// printing the biggest word
}// end of main
}// end of class
Ejemplo 3: java obtiene cuántas letras hay en una cadena
String str = "a string";
int length = str.length( ); // length == 8
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)