* rename file

* create strings directory
* fix docs
* add test
This commit is contained in:
shellhub
2020-08-26 00:05:11 +08:00
parent f90a8bed7e
commit 6356db0b97
7 changed files with 148 additions and 121 deletions

View File

@ -1,24 +0,0 @@
package Others;
/**
* An Abecadrian is a word where each letter is in alphabetical order
*
* @author Oskar Enmalm
*/
class Abecedarian {
public static boolean isAbecedarian(String s) {
int index = s.length() - 1;
for (int i = 0; i < index; i++) {
if (s.charAt(i) <= s.charAt(i + 1)) {
} //Need to check if each letter for the whole word is less than the one before it
else {
return false;
}
}
return true;
}
}

View File

@ -1,43 +0,0 @@
package Others;
/**
* An Armstrong number is equal to the sum of the cubes of its digits.
* For example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370.
* An Armstrong number is often called Narcissistic number.
*/
public class Armstrong {
public static void main(String[] args) {
assert (isArmStrong(0));
assert (isArmStrong(1));
assert (isArmStrong(153));
assert (isArmStrong(1634));
assert (isArmStrong(371));
assert (!isArmStrong(200));
}
/**
* Checks whether a given number is an armstrong number or not.
*
* @param number number to check
* @return {@code true} if given number is armstrong number, {@code false} otherwise
*/
private static boolean isArmStrong(int number) {
int sum = 0;
int temp = number;
int numberOfDigits = 0;
while (temp != 0) {
numberOfDigits++;
temp /= 10;
}
temp = number; /* copy number again */
while (number > 0) {
int remainder = number % 10;
int power = 1;
for (int i = 1; i <= numberOfDigits; power *= remainder, ++i) ;
sum = sum + power;
number /= 10;
}
return sum == temp;
}
}

View File

@ -1,50 +0,0 @@
package Others;
class Palindrome {
private String reverseString(String x) { // *helper method
StringBuilder output = new StringBuilder(x);
return output.reverse().toString();
}
public boolean FirstWay(String x) { // *palindrome method, returns true if palindrome
if (x == null || x.length() <= 1)
return true;
return x.equalsIgnoreCase(reverseString(x));
}
public boolean SecondWay(String x) {
if (x.length() == 0 || x.length() == 1)
return true;
if (x.charAt(0) != x.charAt(x.length() - 1))
return false;
return SecondWay(x.substring(1, x.length() - 1));
}
/**
* This method ignores all non-alphanumeric characters and case runs in O(n)
* where n is the length of s
*
* @param s String to check
* @return true if s is palindrome else false
*/
public boolean isPalindrome(String s) {
s = s.toLowerCase().trim();
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isLetter(c) || Character.isDigit(c))
sb.append(c);
}
s = sb.toString();
int start = 0;
int end = s.length() - 1;
while (start <= end) {
if (s.charAt(start++) != s.charAt(end--))
return false;
}
return true;
}
}

View File

@ -1,46 +0,0 @@
package Others;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* This method produces a reversed version of a string
*
* @author Unknown
*/
public class ReverseString {
/**
* This method reverses the string str and returns it
*
* @param str String to be reversed
* @return Reversed string
*/
public static String reverse(String str) {
if (str == null || str.isEmpty()) return str;
char[] arr = str.toCharArray();
for (int i = 0, j = str.length() - 1; i < j; i++, j--) {
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return new String(arr);
}
/**
* Main Method
*
* @param args Command line arguments
* @throws IOException Exception thrown because of BufferedReader
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the string");
String srr = br.readLine();
System.out.println("Reverse=" + reverse(srr));
br.close();
}
}