How to write an equivalent to PHP count_chars(string $string, int $mode = 0): array|string function in Java

1 Answer

0 votes
public class CharCounter {

    private static final int ASCII_RANGE = 256;

    // Count characters and produce different outputs depending on mode
    // Mode 0,1,2: fill freq array
    // Mode 3,4: fill output string
    public static void countChars(String input, int mode, int[] freq, StringBuilder outStr) {
        // Reset frequencies
        for (int i = 0; i < ASCII_RANGE; i++) {
            freq[i] = 0;
        }

        // Count frequencies
        for (char ch : input.toCharArray()) {
            freq[(int) ch]++;
        }

        outStr.setLength(0); // clear

        if (mode == 3) { // used characters string
            for (int i = 0; i < ASCII_RANGE; i++) {
                if (freq[i] > 0) {
                    outStr.append((char) i);
                }
            }
        } else if (mode == 4) { // unused characters string
            for (int i = 0; i < ASCII_RANGE; i++) {
                if (freq[i] == 0) {
                    outStr.append((char) i);
                }
            }
        }
    }

    // Unified print function
    // mode = 0 → all characters
    // mode = 1 → only used characters
    // mode = 2 → only unused characters
    // printableOnly = true → restrict to ASCII 32–126
    public static void printArray(int[] freq, int mode, boolean printableOnly) {
        int start = printableOnly ? 32 : 0;
        int end   = printableOnly ? 127 : ASCII_RANGE;

        for (int i = start; i < end; i++) {
            boolean condition = true;
            if (mode == 1 && freq[i] <= 0) condition = false; // used only
            if (mode == 2 && freq[i] != 0) condition = false; // unused only

            if (condition) {
                System.out.printf("'%c' (%d): %d%n", (char) i, i, freq[i]);
            }
        }
    }

    public static void main(String[] args) {
        String text = "It's the perfect time to start";
        int[] freq = new int[ASCII_RANGE];
        StringBuilder buffer = new StringBuilder();

        // Mode 1: Only used characters and their frequencies
        System.out.println("Mode 1: Used characters only");
        countChars(text, 1, freq, buffer);
        printArray(freq, 1, true);

        // Mode 2: Unused characters
        System.out.println("\nMode 2: Unused characters");
        countChars(text, 2, freq, buffer);
        printArray(freq, 2, true);

        // Mode 3: String of used characters
        System.out.print("\nMode 3: String of used characters: ");
        countChars(text, 3, freq, buffer);
        System.out.println(buffer.toString());

        // Mode 4: String of unused characters
        System.out.print("\nMode 4: String of unused characters: ");
        countChars(text, 4, freq, buffer);
        // truncate for readability
        String unused = buffer.toString();
        if (unused.length() > 113) {
            System.out.println(unused.substring(0, 113) + "...");
        } else {
            System.out.println(unused);
        }
    }
}



/*
run:

Mode 1: Used characters only
' ' (32): 5
''' (39): 1
'I' (73): 1
'a' (97): 1
'c' (99): 1
'e' (101): 4
'f' (102): 1
'h' (104): 1
'i' (105): 1
'm' (109): 1
'o' (111): 1
'p' (112): 1
'r' (114): 2
's' (115): 2
't' (116): 7

Mode 2: Unused characters
'!' (33): 0
'"' (34): 0
'#' (35): 0
'$' (36): 0
'%' (37): 0
'&' (38): 0
'(' (40): 0
')' (41): 0
'*' (42): 0
'+' (43): 0
',' (44): 0
'-' (45): 0
'.' (46): 0
'/' (47): 0
'0' (48): 0
'1' (49): 0
'2' (50): 0
'3' (51): 0
'4' (52): 0
'5' (53): 0
'6' (54): 0
'7' (55): 0
'8' (56): 0
'9' (57): 0
':' (58): 0
';' (59): 0
'<' (60): 0
'=' (61): 0
'>' (62): 0
'?' (63): 0
'@' (64): 0
'A' (65): 0
'B' (66): 0
'C' (67): 0
'D' (68): 0
'E' (69): 0
'F' (70): 0
'G' (71): 0
'H' (72): 0
'J' (74): 0
'K' (75): 0
'L' (76): 0
'M' (77): 0
'N' (78): 0
'O' (79): 0
'P' (80): 0
'Q' (81): 0
'R' (82): 0
'S' (83): 0
'T' (84): 0
'U' (85): 0
'V' (86): 0
'W' (87): 0
'X' (88): 0
'Y' (89): 0
'Z' (90): 0
'[' (91): 0
'\' (92): 0
']' (93): 0
'^' (94): 0
'_' (95): 0
'`' (96): 0
'b' (98): 0
'd' (100): 0
'g' (103): 0
'j' (106): 0
'k' (107): 0
'l' (108): 0
'n' (110): 0
'q' (113): 0
'u' (117): 0
'v' (118): 0
'w' (119): 0
'x' (120): 0
'y' (121): 0
'z' (122): 0
'{' (123): 0
'|' (124): 0
'}' (125): 0
'~' (126): 0

Mode 3: String of used characters:  'Iacefhimoprst

Mode 4: String of unused characters: 	
!"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHJKLMNOPQRSTUVWXYZ[\]^_`bdgjklnquvwxyz{|}~...

*/

 



answered 3 hours ago by avibootz
...