public boolean isPalindrome(String s) { if (s.length() == 0) return true; // System.out.println(cs); int i = 0, j = s.length() - 1; boolean flag = true; while (i < j) { // System.out.println(s.charAt(i)+" vs " + s.charAt(j) ); if (Character.isLetterOrDigit(s.charAt(i)) == false) { // System.out.println("i is not alphanumeric " + s.charAt(i) ); i++; continue; } if (Character.isLetterOrDigit(s.charAt(j)) == false) { // System.out.println("j is not alphanumeric:" + s.charAt(j) ); j--; continue; } if (s.charAt(i) == s.charAt(j) || Math.abs(s.charAt(i) - s.charAt(j)) == 32) { if ((Character.isDigit(s.charAt(i)) && Character.isLetter(s.charAt(j))) || (Character.isDigit(s.charAt(j)) && Character.isLetter(s.charAt(i)))) { flag = false; break; } flag = true; i++; j--; } else { flag = false; // System.out.println(s.charAt(i)+" vs " + s.charAt(j) ); break; } } System.out.println(flag); return flag; }
public static boolean isFormat(String str, String... format) { if (str.length() == 0) return format.length == 1 && format[0].equals(""); char[] charset = str.toCharArray(); int c = 0; for (int i = 0; i < format.length; i++) { String form = format[i]; if (form.equals(NUMBERS)) { if (c >= charset.length) return false; if (!Character.isDigit(charset[c])) { if (charset[c] == '.') { if (c + 1 >= charset.length || !Character.isDigit(charset[c + 1])) return false; } else return false; } while (c < charset.length - 1 && (Character.isDigit(charset[c + 1]) || charset[c + 1] == '.')) c++; c++; } else if (form.equals(LETTERS)) { if (c >= charset.length || !Character.isLetter(charset[c])) return false; while (c < charset.length - 1 && Character.isLetter(charset[c + 1])) c++; c++; } else { char[] formchars = form.toCharArray(); if (formchars.length > charset.length - c) return false; for (int ch = 0; ch < formchars.length; ch++) { if (formchars[ch] != charset[c]) return false; c++; } } } return c == charset.length; }
public static String getWord(FileReader in) throws IOException { // returns the next word found final int MaxLen = 255; int c, n = 0; char[] word = new char[MaxLen]; // read over non-letters while (!Character.isLetter((char) (c = in.read())) && (c != -1)) ; // empty while body if (c == -1) return ""; // no letter found word[n++] = (char) c; while (Character.isLetter(c = in.read())) if (n < MaxLen) word[n++] = (char) c; return new String(word, 0, n); } // end getWord
/** * @ensures: if input file is read, a String is filled with words from file and searched in * dictionary list, and if found increase words found counter, otherwise increase words not * found. Otherwise error reading file */ public void readFileOliver() { try { FileInputStream inf = new FileInputStream(new File("oliver.txt")); char let; String str = ""; String key = ""; int n = 0; while ((n = inf.read()) != -1) { let = (char) n; if (Character.isLetter(let)) { str += Character.toLowerCase(let); } if ((Character.isWhitespace(let) || let == '-') && !str.isEmpty()) { key = str; str = ""; boolean a = dictionary[(int) key.charAt(0) - 97].contains(key); if (a == true) { counter = dictionary[(int) key.charAt(0) - 97].indexOf(key); counterWFound++; counterWFCompared += counter; } else { counter = dictionary[(int) key.charAt(0) - 97].indexOf( dictionary[(int) key.charAt(0) - 97].getLast()); counterWNotFound++; counterWNFCompared += counter; } } } inf.close(); } catch (IOException e) { e.printStackTrace(); } }
private boolean containsMostlyLetters(BxLine line) { double letterCount = 0; for (char ch : line.toText().toCharArray()) { if (Character.isLetter(ch)) letterCount++; } return 2 * letterCount > line.toText().length(); }
public static int makestring(String a, Token b) { char[] temp = new char[a.length()]; temp = a.toCharArray(); int i = 1; if (temp[0] == '\'') { while (Character.isLetter(temp[i]) || Character.isDigit(temp[i]) || isoperator(temp[i]) || ispunc(temp[i]) || Character.isWhitespace(temp[i])) { i++; if (temp[i - 1] == '\'') { setToken(b, i - 2, a.substring(1, i - 1), LexToInt.string); return i; } if (i == a.length()) { break; } } if (temp[i - 1] == '\'') { setToken(b, i - 2, a.substring(1, i - 1), LexToInt.string); return i; } } return -1; }
/** * Return a set of the variables in the supplied expression. Note: Substitutions which are in the * constant table are not included. */ public Set<String> getVariablesWithin(String exp) { Set<String> all = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); String add = null; if (separators == null) { StringBuilder sep = new StringBuilder(10); for (char chr = 0; chr < operators.length; chr++) { if (operators[chr] != null && !operators[chr].internal) { sep.append(chr); } } sep.append("()"); separators = sep.toString(); } for (StringTokenizer tkz = new StringTokenizer(exp, separators, true); tkz.hasMoreTokens(); ) { String tkn = tkz.nextToken().trim(); if (tkn.length() != 0 && Character.isLetter(tkn.charAt(0))) { add = tkn; } else if (tkn.length() == 1 && tkn.charAt(0) == '(') { add = null; } else if (add != null && !constants.containsKey(add)) { all.add(add); } } if (add != null && !constants.containsKey(add)) { all.add(add); } return all; }
/** Returns the contents of the next text field. */ public String getNextString() { String theText; if (stringField == null) return ""; TextField tf = (TextField) (stringField.elementAt(sfIndex)); theText = tf.getText(); if (macro) { String label = (String) labels.get((Object) tf); theText = Macro.getValue(macroOptions, label, theText); if (theText != null && (theText.startsWith("&") || label.toLowerCase(Locale.US).startsWith(theText))) { // Is the value a macro variable? if (theText.startsWith("&")) theText = theText.substring(1); Interpreter interp = Interpreter.getInstance(); String s = interp != null ? interp.getVariableAsString(theText) : null; if (s != null) theText = s; } } if (recorderOn) { String s = theText; if (s != null && s.length() >= 3 && Character.isLetter(s.charAt(0)) && s.charAt(1) == ':' && s.charAt(2) == '\\') s = s.replaceAll("\\\\", "\\\\\\\\"); // replace "\" with "\\" in Windows file paths if (!smartRecording || !s.equals((String) defaultStrings.elementAt(sfIndex))) recordOption(tf, s); else if (Recorder.getCommandOptions() == null) Recorder.recordOption(" "); } sfIndex++; return theText; }
// by Jaume Ortola private boolean areEqual(final char x, final char y) { if (x == y) { return true; } if (dictionaryMetadata.getEquivalentChars() != null && dictionaryMetadata.getEquivalentChars().containsKey(x) && dictionaryMetadata.getEquivalentChars().get(x).contains(y)) { return true; } if (dictionaryMetadata.isIgnoringDiacritics()) { String xn = Normalizer.normalize(Character.toString(x), Form.NFD); String yn = Normalizer.normalize(Character.toString(y), Form.NFD); if (xn.charAt(0) == yn.charAt(0)) { // avoid case conversion, if possible return true; } if (dictionaryMetadata.isConvertingCase()) { // again case conversion only when needed -- we // do not need String.lowercase because we only check // single characters, so a cheaper method is enough if (Character.isLetter(xn.charAt(0))) { boolean testNeeded = Character.isLowerCase(xn.charAt(0)) != Character.isLowerCase(yn.charAt(0)); if (testNeeded) { return Character.toLowerCase(xn.charAt(0)) == Character.toLowerCase(yn.charAt(0)); } } } return xn.charAt(0) == yn.charAt(0); } return false; }
private String toJavaName(String name, boolean firstLetterIsUpperCase) { name = name.toLowerCase(); StringBuffer res = new StringBuffer(); boolean nextIsUpperCase = firstLetterIsUpperCase; for (int i = 0; i < name.length(); i++) { char c = name.charAt(i); if (nextIsUpperCase) { c = Character.toUpperCase(c); } if (Character.isLetter(c)) { res.append(c); nextIsUpperCase = false; } else { nextIsUpperCase = true; } } return res.toString(); }
/* return string representation of fault code */ public static String GetFaultString(long fault) { if (fault > 0L) { StringBuffer sb = new StringBuffer(); if ((fault & TYPE_MASK) == TYPE_J1708) { // SID: "128/s123/1" // PID: "128/123/1" boolean active = DTOBDFault.DecodeActive(fault); int mid = DTOBDFault.DecodeSystem(fault); int fmi = DTOBDFault.DecodeFMI(fault); if (!active) { sb.append("["); } sb.append(mid); // MID sb.append("/"); if (DTOBDFault.IsJ1708_SID(fault)) { int sid = DTOBDFault.DecodePidSid(fault); sb.append("s").append(sid); // SID "128/s123/1" } else { int pid = DTOBDFault.DecodePidSid(fault); sb.append(pid); // PID "128/123/1" } sb.append("/"); sb.append(fmi); // FMI if (!active) { sb.append("]"); } return sb.toString(); } else if ((fault & TYPE_MASK) == TYPE_J1939) { // SPN: "128/1" boolean active = DTOBDFault.DecodeActive(fault); int spn = DTOBDFault.DecodeSystem(fault); int fmi = DTOBDFault.DecodeFMI(fault); sb.append(spn); // SPN sb.append("/"); sb.append(fmi); // FMI return sb.toString(); } else if ((fault & TYPE_MASK) == TYPE_OBDII) { // DTC: "P0071" [was "024C"] boolean active = DTOBDFault.DecodeActive(fault); int sysChar = DTOBDFault.DecodeSystem(fault); // System: powertrain int subSys = DTOBDFault.DecodeSPID(fault); // Mfg/Subsystem/Problem if (Character.isLetter((char) sysChar)) { sb.append((char) sysChar); } else { sb.append("U"); } if ((subSys & 0x8000) != 0) { sb.append("1"); } else { sb.append("0"); } String subSysStr = String.valueOf(1000 + ((subSys & 0xFFF) % 1000)); sb.append(subSysStr.substring(1)); return sb.toString(); } else { // unrecognized } } return ""; }
public static String getLetters(String s) { String str = ""; for (int i = 0; i < s.length(); i++) { if (Character.isLetter(s.charAt(i))) str += s.charAt(i); } return str; }
private String variableNameToPropertyNameInner(String name, VariableKind variableKind) { String prefix = getPrefixByVariableKind(variableKind); String suffix = getSuffixByVariableKind(variableKind); boolean doDecapitalize = false; int pLength = prefix.length(); if (pLength > 0 && name.startsWith(prefix) && name.length() > pLength && // check it's not just a long camel word that happens to begin with the specified prefix (!Character.isLetter(prefix.charAt(pLength - 1)) || Character.isUpperCase(name.charAt(pLength)))) { name = name.substring(pLength); doDecapitalize = true; } if (name.endsWith(suffix) && name.length() > suffix.length()) { name = name.substring(0, name.length() - suffix.length()); doDecapitalize = true; } if (doDecapitalize) { name = Introspector.decapitalize(name); } return name; }
public static void main(String args[]) { Scanner in = new Scanner(System.in); int n = in.nextInt(); String text = in.next(); char[] input = new char[text.length()]; int k = in.nextInt(); for (int i = 0; i < text.length(); ++i) input[i] = text.charAt(i); for (int i = 0; i < text.length(); ++i) { if (Character.isLetter(input[i])) { int value = (int) input[i] + k % 26; if (Character.isLowerCase(input[i])) { if (value > (int) 'z') value = value - 26; } else if (Character.isUpperCase(input[i])) { if (value > (int) 'Z') value = value - 26; } input[i] = (char) value; } } for (int i = 0; i < n; ++i) System.out.print(input[i]); System.out.println(); }
public static String getDataElementName(String widgetName) { if (widgetName == null || widgetName.length() == 0) { return null; } String[] nameParts = widgetName.split("_"); String convertedName = null; // making widget name camel case for (String namePart : nameParts) { int i; for (i = 0; i < namePart.length(); i++) { char c = namePart.charAt(i); if (!Character.isLetter(c) || Character.isLowerCase(c)) { break; } } namePart = namePart.substring(0, i).toLowerCase() + namePart.substring(i); if (convertedName == null) { convertedName = namePart; } else { convertedName += "_" + namePart; } } if (convertedName == null) { return null; } return convertedName.replaceAll(" ", "").replaceAll("-", ""); }
public static boolean isWindowsAbsolutePath(@NotNull String pathString) { if (pathString.length() >= 2 && Character.isLetter(pathString.charAt(0)) && pathString.charAt(1) == ':') { return true; } return false; }
public static String dropNonLetters(String s) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (Character.isLetter(c)) sb.append(c); } return sb.toString(); }
/** * Returns true if <code>str</code> is made up of all-lowercase characters (ignoring characters * for which no upper-/lowercase distinction exists). */ boolean isNotAllLowercase(final String str) { for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (Character.isLetter(c) && !Character.isLowerCase(c)) { return true; } } return false; }
public static boolean isFunctionName(String str) { /* * actually checks if all the chars are letters */ for (char c : str.toCharArray()) { if (!Character.isLetter(c)) return false; } return true; }
public static int makeIdentifier(String a, Token b) { final char under = '_'; char[] temp = new char[a.length()]; temp = a.toCharArray(); int i = 0; if (Character.isLetter(temp[i])) { while (Character.isLetter(temp[i]) || Character.isDigit(temp[i]) || temp[i] == under) { i++; if (i == a.length()) { break; } } setToken(b, i, a.substring(0, i), LexToInt.identifier); detailID(b); return i; } return -1; }
private void validateName(String nam) { if (!Character.isLetter(nam.charAt(0))) { throw new IllegalArgumentException( "Names for constants, variables and functions must start with a letter"); } if (nam.indexOf('(') != -1 || nam.indexOf(')') != -1) { throw new IllegalArgumentException( "Names for constants, variables and functions may not contain a parenthesis"); } }
@SuppressWarnings({"HardCodedStringLiteral"}) @Nullable public static VirtualFile findRelativeFile(@NotNull String uri, @Nullable VirtualFile base) { if (base != null) { if (!base.isValid()) { LOG.error("Invalid file name: " + base.getName() + ", url: " + uri); } } uri = uri.replace('\\', '/'); if (uri.startsWith("file:///")) { uri = uri.substring("file:///".length()); if (!SystemInfo.isWindows) uri = "/" + uri; } else if (uri.startsWith("file:/")) { uri = uri.substring("file:/".length()); if (!SystemInfo.isWindows) uri = "/" + uri; } else if (uri.startsWith("file:")) { uri = uri.substring("file:".length()); } VirtualFile file = null; if (uri.startsWith("jar:file:/")) { uri = uri.substring("jar:file:/".length()); if (!SystemInfo.isWindows) uri = "/" + uri; file = VirtualFileManager.getInstance().findFileByUrl(JarFileSystem.PROTOCOL_PREFIX + uri); } else { if (!SystemInfo.isWindows && StringUtil.startsWithChar(uri, '/')) { file = LocalFileSystem.getInstance().findFileByPath(uri); } else if (SystemInfo.isWindows && uri.length() >= 2 && Character.isLetter(uri.charAt(0)) && uri.charAt(1) == ':') { file = LocalFileSystem.getInstance().findFileByPath(uri); } } if (file == null && uri.contains(JarFileSystem.JAR_SEPARATOR)) { file = JarFileSystem.getInstance().findFileByPath(uri); if (file == null && base == null) { file = VirtualFileManager.getInstance().findFileByUrl(uri); } } if (file == null) { if (base == null) return LocalFileSystem.getInstance().findFileByPath(uri); if (!base.isDirectory()) base = base.getParent(); if (base == null) return LocalFileSystem.getInstance().findFileByPath(uri); file = VirtualFileManager.getInstance().findFileByUrl(base.getUrl() + "/" + uri); if (file == null) return null; } return file; }
/** @param str input string */ boolean isNotCapitalizedWord(final String str) { if (isNotEmpty(str) && Character.isUpperCase(str.charAt(0))) { for (int i = 1; i < str.length(); i++) { char c = str.charAt(i); if (Character.isLetter(c) && !Character.isLowerCase(c)) { return true; } } return false; } return true; }
/* Following are the method for Lookup Device name*/ private static String LookupDevice(String substring) { // TODO Auto-generated method stub File p = new File( "/Users/luozhongyi/Desktop/2015 Fall course/CS 6400/MacPrefix2.txt"); // The path of // MacPrefix.txt Scanner pcon = null; try { pcon = new Scanner(p); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String pcurrentline; String Device = null; char[] prefix = {'0', '0', '0', '0', '0', '0', '0', '0'}; // char px; int flag = 0; // prefix=substring.substring(0, 8); // rember to transfer to uppercase // System.out.println(substring); for (int i = 0; i < 8; i++) { // System.out.println(i+""); // System.out.println(Character.toUpperCase(substring.charAt(i))); if (Character.isLetter(substring.charAt(i))) prefix[i] = Character.toUpperCase(substring.charAt(i)); else // System.out.println(i+""); // System.out.println(substring.charAt(i)); prefix[i] = substring.charAt(i); } // get the uppercase of prefix /* Find in MacPrefix txt */ while (pcon.hasNextLine()) { pcurrentline = pcon.nextLine().toString(); if (pcurrentline.startsWith(new String(prefix))) { // find the first match and return flag = 1; pcurrentline = pcurrentline.replaceAll("\\s+", " "); // delete duplicate " "; // System.out.println(pcurrentline); Device = pcurrentline.substring( pcurrentline.indexOf(' ') + 1, pcurrentline.indexOf(' ', pcurrentline.indexOf(' ') + 1)); } else ; } if (flag == 0) Device = "DEFAULT"; // System.out.println(Device); pcon.close(); return Device; }
public Field(String s) { String f[] = StringTools.parseString(s, FIELD_VALUE_SEPARATOR); if ((f.length > 0) && (f[0].length() > 0) && Character.isLetter(f[0].charAt(0))) { this.isHiRes = (f.length > 0) ? f[0].equalsIgnoreCase("H") : false; this.type = (f.length > 1) ? StringTools.parseInt(f[1], -1) : -1; } else { this.type = (f.length > 0) ? StringTools.parseInt(f[0], -1) : -1; this.isHiRes = (f.length > 1) ? f[1].equalsIgnoreCase("H") : false; } this.index = (f.length > 2) ? StringTools.parseInt(f[2], 0) : 0; this.length = (f.length > 3) ? StringTools.parseInt(f[3], 0) : 0; this.isValid = (f.length == 4) && (this.type >= 0) && (this.index >= 0) && (this.length > 0); }
/** * @param lcphrase Some English text in lowercase. * @return An array of booleans, one per character of the input, indicating whether it would be OK * to insert a hyphen before that character. */ public boolean[] findBreakPoints(char[] lcphrase) { boolean[] breakPoints = new boolean[lcphrase.length]; boolean inWord = false; int wordStart = 0; int c = 0; for (; c < lcphrase.length; ++c) { if (!inWord && Character.isLetter(lcphrase[c])) { wordStart = c; inWord = true; } else if (inWord && !Character.isLetter(lcphrase[c])) { inWord = false; labelWordBreakPoints(lcphrase, wordStart, c, breakPoints); } } if (inWord) { labelWordBreakPoints(lcphrase, wordStart, c, breakPoints); } return breakPoints; }
public static String asTag(String n) { StringBuffer result = new StringBuffer(n.length()); for (int i = 0; i < n.length(); i++) { char c = n.charAt(i); if (Character.isLetter(c)) { result.append(Character.toLowerCase(c)); } else if (Character.isDigit(c)) { result.append(c); } } return result.toString(); }
private static boolean go(int a, int b) { if (memo[a][b] >= 0) return memo[a][b] == 1; if (a == b) return true; if (b - a == 1) return str[a] == ' ' || str[a] == ':' || Character.isLetter(str[a]); boolean ok = false; if (b - a == 2) ok |= str[a] == ':' && (str[a + 1] == ')' || str[a + 1] == '('); if (b - a >= 2 && str[a] == '(' && str[b - 1] == ')') ok |= go(a + 1, b - 1); for (int x = a + 1; x < b && !ok; x++) ok |= go(a, x) && go(x, b); memo[a][b] = ok ? 1 : 0; return ok; }
public static ArrayList<Symbol> toSymbols(String expression) { ArrayList<Symbol> symbols = new ArrayList<>(); for (int i = 0; i < expression.length(); i++) { char c = expression.charAt(i); if (isBracket(c)) { // bracket symbols.add(new Symbol(c)); } else if (c == ',') { symbols.add(new Symbol(c)); } else if (isOperator(c)) { if (c == '-') { if (i == 0) { // must be first +/-, so add 0 symbols.add(new Symbol(BigDecimal.valueOf(0))); symbols.add(new Symbol(c)); } else { char previous = expression.charAt(i - 1); if ((previous == ')') || isNumber(String.valueOf(previous))) { // must be "minus" // e.g. sin(3)-5, 100-5, symbols.add(new Symbol(c)); } else if ((previous == '(') || (previous == ',')) { // must be "negative" // e.g. (-3+5)/2 symbols.add(new Symbol(BigDecimal.valueOf(0))); symbols.add(new Symbol(c)); } else if ((previous == '*') || (previous == '/') || (previous == '^')) { int end = Symbol.getNumberEndAt(i + 1, expression); symbols.add(new Symbol(new BigDecimal(expression.substring(i, end)))); i = end - 1; } } } else { symbols.add(new Symbol(c)); } } else if (Character.isLetter(c)) { // function int end = Symbol.getFunctionEndAt(i, expression); symbols.add(new Symbol(expression.substring(i, end))); i = end - 1; } else if (Character.isDigit(c)) { int end = Symbol.getNumberEndAt(i, expression); String number = expression.substring(i, end); symbols.add(new Symbol(new BigDecimal(number))); i = end - 1; } } return symbols; }
/** Counts words in a note */ public int getWords(Note note) { int count = 0; String[] fields = {note.getTitle(), note.getContent()}; for (String field : fields) { boolean word = false; int endOfLine = field.length() - 1; for (int i = 0; i < field.length(); i++) { // if the char is a letter, word = true. if (Character.isLetter(field.charAt(i)) && i != endOfLine) { word = true; // if char isn't a letter and there have been letters before, // counter goes up. } else if (!Character.isLetter(field.charAt(i)) && word) { count++; word = false; // last word of String; if it doesn't end with a non letter, it // wouldn't count without this. } else if (Character.isLetter(field.charAt(i)) && i == endOfLine) { count++; } } } return count; }