Created
July 4, 2021 19:30
-
-
Save SoPat712/7f3675f58884c75c8a8ead5dca3b2a91 to your computer and use it in GitHub Desktop.
Java Intermediate: 07/01/2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javafx.test; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("Enter a password to check if it's valid: "); | |
String str123 = scanner.nextLine(); | |
System.out.println(); | |
isValid(str123); | |
System.out.print("Enter a word to check if it's a palindrome: "); | |
String word = scanner.nextLine().trim().toLowerCase(); | |
palindrome(word); | |
System.out.print("Enter a sentence to reverse the characters in each word: "); | |
String str = scanner.nextLine(); | |
wordReverse(str); | |
System.out.print("Enter a string to concatenate with itself: "); | |
String str1 = scanner.nextLine(); | |
System.out.print("Enter a number of times to concatenate it: "); | |
int int1 = scanner.nextInt(); | |
String resultV1 = repeat_str(str1, int1); | |
System.out.println("\nAfter repeating "+int1+" times: "+resultV1); | |
System.out.println("Average of 3 numbers: "+ avg(5,10,15)); | |
} | |
public static void wordReverse(String str){ | |
String[] words = str.split(" "); | |
String reversedString = ""; | |
for (int i = 0; i < words.length; i++) | |
{ | |
String word = words[i]; | |
String reverseWord = ""; | |
for (int j = word.length()-1; j >= 0; j--) | |
{ | |
/* The charAt() function returns the character | |
* at the given position in a string | |
*/ | |
reverseWord = reverseWord + word.charAt(j); | |
} | |
reversedString = reversedString + reverseWord + " "; | |
} | |
System.out.println(reversedString); | |
} | |
public static void palindrome(String word){ | |
int low = 0; // low index of the word | |
int high = word.length() - 1; // high index of the word | |
boolean isPalindrome = true; | |
if (word.isEmpty()) { | |
isPalindrome = false; | |
} else { | |
while (low < high) { | |
// check if chars at low and high indices are matching | |
if (word.charAt(low) != word.charAt(high)) { | |
isPalindrome = false; | |
break; | |
} | |
low++; | |
high--; | |
} | |
} | |
if (isPalindrome) { | |
System.out.println("The word is a palindrome."); | |
} else { | |
System.out.println("The word is NOT a palindrome."); | |
} | |
} | |
public static String repeat_str(String str1, int n) { | |
if (str1 == null || str1.isEmpty()) { | |
return ""; | |
} | |
if (n <= 0) { | |
return str1; | |
} | |
String newString = ""; | |
for(int i = 1; i <= n; i++){ | |
newString = newString + str1; | |
} | |
return newString; | |
} | |
public static double avg(int a, int b, int c){ | |
int total = a+b+c; | |
double average = total/3; | |
return average; | |
} | |
public static void isValid(String password) | |
{ | |
boolean valid = true; | |
// for checking if password length | |
// is between 8 and 15 | |
if (!((password.length() >= 8) | |
)) { | |
valid = false; | |
} | |
// to check space | |
if (password.contains(" ")) { | |
valid = false; | |
} | |
if (true) { | |
int count = 0; | |
// check digits from 0 to 9 | |
for (int i = 0; i <= 9; i++) { | |
// to convert int to string | |
String str1 = Integer.toString(i); | |
if (password.contains(str1)) { | |
count = 1; | |
} | |
} | |
if (count == 0) { | |
valid = false; | |
} | |
} | |
// for special characters | |
if (!(password.contains("@") || password.contains("#") | |
|| password.contains("!") || password.contains("~") | |
|| password.contains("$") || password.contains("%") | |
|| password.contains("^") || password.contains("&") | |
|| password.contains("*") || password.contains("(") | |
|| password.contains(")") || password.contains("-") | |
|| password.contains("+") || password.contains("/") | |
|| password.contains(":") || password.contains(".") | |
|| password.contains(", ") || password.contains("<") | |
|| password.contains(">") || password.contains("?") | |
|| password.contains("|"))) { | |
valid = false; | |
} | |
// if all conditions fails | |
if(valid){ | |
System.out.println("This password is valid"); | |
} | |
else { | |
System.out.println("This password is not valid"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment