Java method to count the number of characters and reverse order different characters

  • 2020-04-01 02:02:54
  • OfStack


import java.util.ArrayList;
import java.util.List;
public class Test2 {
 
 public static void main(String[] args) {
  String src = "A B C D E B C";
  //Replace the space
  src = src.replaceAll(" ", "") ;

  System.out.println(" String after removing Spaces: " + src) ;
  List<Character> list = new ArrayList<Character>() ;
  int[] bb = new int[256];   
  char[] cs = src.toCharArray();   

  //Reverse order
  int mid = cs.length / 2 ;
  int idx = cs.length -1 ;
  for (int i = 0; i < mid ; i++){
   char tmp = cs[i] ;
   cs[i] = cs[idx] ;
   cs[idx] = tmp ;
   idx-- ;
  }
  //Count and filter the same
  for (char c : cs) {  
   if (bb[c] <1) {
    list.add(c) ;
   }
   bb[c] = bb[c] + 1;   
     } 
  System.out.println();
  for (int i = 0; i < list.size(); i++){
   System.out.print(list.get(i)) ;
  }
  System.out.println() ;

  for (int i = 0; i < list.size(); i++){
   char c = list.get(i) ;

   System.out.println(c + " " + bb[c] + " time ") ;
  }

 }
}

String a = "abcd, efg";
String b = "(* & ^ % $# @! [] {},. / /; : '? < >" ;
The requirement is to determine whether any character in String a appears in String b, the more efficient the better
 
  * finds whether certain characters appear in another string

 *  
 * @author Java people (java2000.net) 
*/  
public class Test {  
    
  public static void main(String[] args) {  
    String a = "abcd,efg";  
    String b = ")(*&^%$#@![]{},.///;:'? <>";  
    byte[] bb = new byte[256];  
    char[] cs = b.toCharArray();  
    for (char c : cs) {  
      bb[c] = 1;  
    }  
    cs = a.toCharArray();  
    for (char c : cs) {  
      if (bb[c] == 1) {  
        System.out.println(c);  
      }  
    }  
  }  
}  


Related articles: