Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 9th, 2012  |  syntax: None  |  size: 1.46 KB  |  hits: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. public class Stringtest {
  3.  
  4.  
  5.  
  6.         public static int subStrC(String s, char c)
  7.         {
  8.                 int scount=0 ,  bounds=s.length()-1;
  9.                 boolean flag =false;
  10.                 for(int i=0; i < s.length() ; i++)
  11.                 {
  12.                         if(!flag){
  13.                                 if(i < bounds-1){
  14.                                         if(s.charAt(i) == c && s.charAt(i+1) != c){
  15.  
  16.                                                 flag = true;
  17.  
  18.                                                 scount++;
  19.                                         }      
  20.                                 }
  21.                         }
  22.                         else if (flag)
  23.                         {
  24.                                 if(i < bounds-1){
  25.                                         if(s.charAt(i) == c && s.charAt(i+1) != c){
  26.                                                 flag=false;
  27.  
  28.                                                 i--;
  29.                                         }
  30.                                 }
  31.                         }
  32.  
  33.                 }
  34.  
  35.                 return scount;
  36.         }
  37.  
  38.  
  39.  
  40.         public static int subStrMaxC(String s, char c, int k){
  41.                 int scount=0 , ccount=0;
  42.                 boolean flag =false;
  43.                 if(k <= 1 )
  44.                         return subStrC(s, c);
  45.                 else {
  46.                         scount = subStrC(s, c);
  47.                         for(int i=0 ; i < s.length()-1 ; i++)
  48.                         {
  49.                                 if(!flag){
  50.                                         if(s.charAt(i) == c && s.charAt(i+1) != c ){
  51.                                                 flag= true;
  52.                                                 scount++;
  53.  
  54.                                         }
  55.                                 }
  56.                                 else if(flag)
  57.                                 {
  58.                                         if(s.charAt(i) == c && s.charAt(i+1) != c){
  59.                                                 if(ccount+1 < k)
  60.                                                         ccount++;
  61.                                                 else if (ccount+1 >= k)
  62.                                                 {
  63.                                                         i--;
  64.                                                         flag= false;
  65.                                                         ccount =0;
  66.  
  67.                                                 }
  68.  
  69.  
  70.                                         }
  71.                                 }
  72.  
  73.  
  74.                         }
  75.  
  76.  
  77.                         return scount;
  78.                 }
  79.         }
  80.  
  81.  
  82.  
  83.         public static void main (String [] arg){
  84.  
  85.                 System.out.println(subStrC("abcbc",'c'));
  86.                 System.out.println(subStrMaxC("abcbc",'c',0));
  87.                 System.out.println(subStrMaxC("abcbcabcacab", 'c', 2));
  88.                 System.out.println(subStrMaxC("abcbcabcacab", 'c', 3));
  89.                 System.out.println(subStrMaxC("abc", 'c', 0));
  90.         }
  91.  
  92. }