fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static int buscaChar(String text, char c, int n) {
  11. int count = 0;
  12. for (int i = 0; i < text.length() - 1; i++) {
  13. if (text.charAt(i) == c) {
  14. count = count + 1;
  15. if (count == n)
  16. return i;
  17. }
  18. }
  19. return -1;
  20. }
  21.  
  22. public static void main (String[] args) throws java.lang.Exception
  23. {
  24. String textCopy = "A linguagem processing e a mais divertida.";
  25.  
  26. int test = buscaChar(textCopy, 'a', 4);
  27.  
  28. System.out.println(test);
  29. }
  30. }
Success #stdin #stdout 0.09s 27780KB
stdin
Standard input is empty
stdout
40