Skip to content

Instantly share code, notes, and snippets.

@donpandix
Last active March 24, 2025 17:08
Show Gist options
  • Save donpandix/c08925bcbf7503dcde45930285539733 to your computer and use it in GitHub Desktop.
Save donpandix/c08925bcbf7503dcde45930285539733 to your computer and use it in GitHub Desktop.
Cuenta la cantidad de caracteres en una cadena de texto eficientemente
class StringHelper {
public static void main(String[] args) {
String cadena = "Hola mundo";
System.out.println( conteoCaracteres('o', cadena));
}
public static int conteoCaracteres(char caracter, String cadena) {
int contador = 0;
int posicion = 0;
while ((posicion = cadena.indexOf(caracter, posicion)) != -1) {
posicion++;
contador++;
}
return contador;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment