Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
887 B
0
Indexable
Never
import java.util.Scanner;

public class NonRepeatedCharacters {
    public static int countNonRepeatedChars(String str) {
        int count = 0;
        int[] freq = new int[256]; // Assuming ASCII characters

        // Count the frequency of each character in the string
        for (char ch : str.toCharArray()) {
            freq[ch]++;
        }

        // Count the characters with frequency 1
        for (int i = 0; i < str.length(); i++) {
            if (freq[str.charAt(i)] == 1) {
                count++;
            }
        }

        return count;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = scanner.nextLine();

        int result = countNonRepeatedChars(str);
        System.out.println("Count of non-repeated characters: " + result);
    }
}