Last active
March 24, 2025 17:08
-
-
Save donpandix/c08925bcbf7503dcde45930285539733 to your computer and use it in GitHub Desktop.
Cuenta la cantidad de caracteres en una cadena de texto eficientemente
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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