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

Run it Now
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("\nSpecial Charactetrs :" + String.valueOf(b));
        } else {
            System.out.print("\nNo Any Special Charactetrs");
        }

    }
}

Output:

Enter a String :Xiith4 454$%^&*
Special Charactetrs :$%^&*