Java Program to find special characters in a string


In this program, You will learn how to find special characters in a string in java.


john#23@smith

Special charactetrs is: #@

Example: How to find special characters in a string in java.

import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        Scanner s = new Scanner(System.in);
        String str;
        char b[] = new char[100];
        System.out.print("Enter a string:");
        str = s.nextLine();

        int i, p, c = 0;
        char a[] = str.toCharArray();
        int l = a.length;

        for (i = 0; i < l; i++) {
            p = a[i];
            if (p >= 65 && p <= 90 || p >= 97 && p <= 122 || p == 32 || p >= 48 && p <= 57) {
                continue;
            } else {
                b[c] = a[i];
                c++;
            }
        }


        if (c > 0) {
            System.out.print("Special characters :" + String.valueOf(b));
        } else {
            System.out.print("No any special characters");
        }

    }
}

Output:

Enter a string:Xiith#@34
Special characters :#@