package test2; public class Utility2B { private Utility2B() { } /** * Returns the string formed by concatenating the argument * strings s and t with a separator string between them. * For example: * *
* Utility2B.join("abc", "xyz", ":") ** * returns the string
"abc:xyz"
.
*
* @param s a non-null string
* @param t a non-null string
* @param sep a non-null separator string
* @return the string formed by concatenating s followed by
* sep followed by t
*/
public static String join(String s, String t, String sep) {
return s + sep + t;
}
/**
* Computes the number of characters that are different in
* two strings of equal length. The strings are compared
* character by character and the number of characters that
* differ is returned. For example:
*
* * Utility2B.distance("talk", "talk") returns 0 * Utility2B.distance("talk", "walk") returns 1 * Utility2B.distance("well", "walk") returns 2 * Utility2B.distance("pick", "walk") returns 3 * Utility2B.distance("zzzz", "walk") returns 4 ** * @param s a non-null string * @param t a non-null string * @return the number of characters that differ between s and t * @throws IllegalArgumentException if s and t have different lengths */ public static int distance(String s, String t) { if (s.length() != t.length()) { throw new IllegalArgumentException(); } int d = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != t.charAt(i)) { d++; } } return d; } /** * Returns true if a string is a palindrome. A string is a * palindrome if it contains the same sequence of characters * when read forwards or backwards. For example, the following * strings are all palindromes: * *
* "noon" * "level" * "racecar" ** *
* The empty string is considered to be a palindrome. * * @param s a non-null string * @return true if s is a palindrome and false otherwise */ public static boolean isPalindrome(String s) { /* int n = s.length() / 2; for (int i = 0; i < n; i++) { char c = s.charAt(i); char d = s.charAt(s.length() - 1 - i); if (c != d) { return false; } } return true; */ int k = s.length(); int counter = 0; int u = k-1; for (int i =0; i<=(k-1); i++){ if (s.charAt(i)==s.charAt(u-i)){ counter += 1; } } if(counter==k){ return true; }return false; } }