Get Answers to all your Questions

header-bg qa

Which is the function to display a group of characters in java

Answers (1)

best_answer

// Java program to print all occurrences of every character 
// together. 

class Test 
 
    // Since only lower case characters are there 
    static final int MAX_CHAR = 26; 
    
    // Method to print the string 
    static void printGrouped(String str) 
     
        int n = str.length(); 
    
        // Initialize counts of all characters as 0 
        int count[] = new int[MAX_CHAR]; 
    
        // Count occurrences of all characters in string 
        for (int i = 0 ; i < n ; i++) 
            count[str.charAt(i)-'a']++; 
    
        // Starts traversing the string 
        for (int i = 0; i < n ; i++) 
         
            // Print the character till its count in 
            // hash array 
            while (count[str.charAt(i)-'a']!=0) 
                System.out.print(str.charAt(i)); 
                count[str.charAt(i)-'a']--; 
             
    
            // Make this character's count value as 0. 
            count[str.charAt(i)-'a'] = 0; 
         
     
    
    // Driver method 
    public static void main(String args[]) 
     
        String str = new String("geeksforgeeks"); 
        
        printGrouped(str); 
     
 
 

Posted by

Deependra Verma

View full answer