Untitled

mail@pastecode.io avatar
unknown
plain_text
4 months ago
1.1 kB
4
Indexable
import java.io.*;
import java.util.*;

class UserMainCode {

    // Helper function to check if a number is prime
    public static boolean isPrime(int num) {
        if (num <= 1) return false;
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) return false;
        }
        return true;
    }

    public static String PrimeChars(String input1) {
        StringBuilder primeChars = new StringBuilder();
        int sum = 0;
        
        // Loop through each character in the input string
        for (char ch : input1.toCharArray()) {
            int asciiValue = (int) ch;
            // Check if ASCII value is prime
            if (isPrime(asciiValue)) {
                primeChars.append(ch);
                sum += asciiValue;
            }
        }

        // Case 1: If prime characters are found
        if (primeChars.length() > 0) {
            return primeChars.toString() + ":" + sum;
        }
        
        // Case 2: If no prime ASCII values are found
        return input1.charAt(0) + ":" + input1.charAt(input1.length() - 1);
    }
}
Leave a Comment