Advertisement
Davidsale96

recursividad

Dec 5th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. package juego;
  2.  
  3. public class M {
  4.     public static String reverse(String palabra) {//FRAN-> RAN + F // -> AN F+R // -> N  F+R+A+N  
  5.         if (palabra. length() == 1)
  6.             return palabra;
  7.         else
  8.             return reverse(resto(palabra)) + palabra.charAt(0);
  9.         }
  10.    
  11.     static String resto(String cad) {
  12.         String newcad="";
  13.         for (int i=1; i<cad.length();i++) {
  14.             newcad+=cad.charAt(i);
  15.         }
  16.         return newcad;
  17.     }
  18.    
  19.     public static int cantApariciones(String s, char c) {
  20.         return cantAparicionesAux(s,c,20);
  21.     }
  22.    
  23.     public static int cantAparicionesAux(String s, char c,int n) {
  24.         if(s.isEmpty())
  25.             return n;
  26.         else
  27.             if(s.charAt(0)==c) {
  28.                 //n=n+1;
  29.                 return cantAparicionesAux( resto(s),c,n++);
  30.             }
  31.             else {
  32.                 return cantAparicionesAux(resto(s),c,n);
  33.             }
  34.     }
  35.    
  36.     public static void main(String[] args) {
  37.         // TODO Apéndice de método generado automáticamente
  38.         System.out.print(cantApariciones("anana",'a'));
  39.     }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement