Uploaded by FRANK

APP WEEK 4 352

advertisement
ADVANCE PROGRAMMING PRACTICES
WEEK 4
RA2211026010352
5. Write a JAVA program that accepts a string and calculate the number
of digits and letters.
Sample Data : SRMIST JULY 2023
Expected Output :
Letters 10
Digits 4
Program;
import java.util.Scanner;
public class CountDigitsAndLetters {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
int letterCount = 0;
int digitCount = 0;
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (Character.isLetter(ch)) {
letterCount++;
} else if (Character.isDigit(ch)) {
digitCount++;
}
}
System.out.println("Letters " + letterCount);
System.out.println("Digits " + digitCount);
scanner.close();
}
}
Output;
6. Write a JAVA program to check the validity of password input by
users.
Validation :
● At least 1 letter between [a-z] and 1 letter between [A-Z].
● At least 1 number between [0-9].
● At least 1 character from [$#@].
● Minimum length 6 characters.
● Maximum length 16 characters.
Sample output
Input your password srmist@2017
Not a Valid Password
Input your password Srmist@2022
Valid Password
Program;
import java.util.Scanner;
public class PasswordValidator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Input your password: ");
String password = scanner.nextLine();
if (isValidPassword(password)) {
System.out.println("Valid Password");
} else {
System.out.println("Not a Valid Password");
}
scanner.close();
}
public static boolean isValidPassword(String password) {
if (password.length() < 6 || password.length() > 16) {
return false;
}
boolean hasLowerCase = false;
boolean hasUpperCase = false;
boolean hasDigit = false;
boolean hasSpecialChar = false;
for (char ch : password.toCharArray()) {
if (Character.isLowerCase(ch)) {
hasLowerCase = true;
} else if (Character.isUpperCase(ch)) {
hasUpperCase = true;
} else if (Character.isDigit(ch)) {
hasDigit = true;
} else if (ch == '$' || ch == '#' || ch == '@') {
hasSpecialChar = true;
}
if (hasLowerCase && hasUpperCase && hasDigit && hasSpecialChar) {
return true;
}
}
return false;
}
}
Output;
7. Write a JAVA program to find numbers between 100 and 400 (both included) where each
digit of a number is an even number. The numbers obtained should be printed in a commaseparated sequence.
Program;
public class EvenDigitNumbers {
public static void main(String[] args) {
System.out.println("Numbers with all even digits between 100 and 400:");
for (int num = 100; num <= 400; num++) {
if (hasAllEvenDigits(num)) {
System.out.print(num);
if (num < 400) {
System.out.print(", ");
}
}
}
}
public static boolean hasAllEvenDigits(int num) {
while (num > 0) {
int digit = num % 10;
if (digit % 2 != 0) {
return false;
}
num /= 10;
}
return true;
}
}
Output;
Download