DEV Community

Cover image for Remove Vowel-java,PasswordValidator,Regex,Password Valitation
Neelakandan R
Neelakandan R

Posted on

Remove Vowel-java,PasswordValidator,Regex,Password Valitation

1)Remove Vowel

package pratice;

import java.util.Scanner;

public class Vowel {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        System.out.println("Enter word :");
        String sh=sc.nextLine();
        for(int i=0;i<sh.length();i++)
        {
            char ch=sh.charAt(i);
            if(!(ch=='a' && ch=='A'||ch=='e' && ch=='E'||ch=='i' && ch=='I'||ch=='o' && ch=='O'||ch=='u' && ch=='U'))
            {
                System.out.print(ch+" ");
            }
        }
    }

}

Enter fullscreen mode Exit fullscreen mode

Output:

Enter word :
salman guruji
s l m n g r j

2)Find Vowel:

package pratice;

import java.util.Scanner;

public class Vowel {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter word :");
        String sh = sc.nextLine();
        for (int i = 0; i < sh.length(); i++) {
            char ch = sh.charAt(i);
            if ((ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O'
                    || ch == 'u' || ch == 'U')) {
                System.out.print(ch + " ");
            }
        }
    }

}
Enter fullscreen mode Exit fullscreen mode

Output:
Enter word :
salman guruji
a a u u i

3)PasswordValidator

package pratice;

public class PasswordValidatorSimple {
    public static void main(String[] args) {
        String password = "Gbc123@";

        boolean upper = false;
        boolean lower = false;
        boolean number = false;
        boolean special = false;

        for (int i = 0; i < password.length(); i++) {
            char ch = password.charAt(i);

            if (ch >= 'A' && ch <= 'Z') {
                upper = true;
            } else if (ch >= 'a' && ch <= 'z') {
                lower = true;
            } else if (ch >= '0' && ch <= '9') {
                number = true;
            } else {
                special = true;
            }
        }

        if (upper && lower && number && special == true) {
            System.out.println("Password is valid");
        } else {
            System.out.println("Password is invalid");
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:
Password is valid

4)Regex

Explanation of the Regex:

PR → starts with PR
\d → digit (0–9), double backslash because Java needs to escape \
{6} → exactly 6 digits

public class PRFormatChecker {
    public static void main(String[] args) {
        String input = "PR123456"; 

        String pattern = "PR\\d{6}";

        // Check if the input matches the pattern
        if (input.matches(pattern)) {
            System.out.println("Valid format");
        } else {
            System.out.println("Invalid format");
        }
    }
}

Enter fullscreen mode Exit fullscreen mode
Input Output
PR123456 Valid format
PR12345 Invalid format
PX123456 Invalid format
PR123A56 Invalid format

5)Password Valitation:

public class PasswordValidatorSimple {
    public static void main(String[] args) {
        String password = "Abc123@";

        boolean upper = false;
        boolean lower = false;
        boolean number = false;
        boolean special = false;

        for (int i = 0; i < password.length(); i++) {
            char ch = password.charAt(i);

            if (ch >= 'A' && ch <= 'Z') {
                upper = true;
            } else if (ch >= 'a' && ch <= 'z') {
                lower = true;
            } else if (ch >= '0' && ch <= '9') {
                number = true;
            } else {
                special = true;
            }
        }

        if (upper && lower && number && special) {
            System.out.println("Password is valid");
        } else {
            System.out.println("Password is invalid");
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

Output:

Password is valid

Top comments (0)