⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise7_17.java

📁 java程序设计 机械工业出版社 书籍代码
💻 JAVA
字号:
/** Exercise7_17.java:
 *  Rewrite Example 7.1, "Checking Palindromes,"
 *  by passing the string as a command-line argument.
 */
public class Exercise7_17 {
  public static void main(String[] args) {
    // Check command-line arguments
    if (args.length != 1) {
      System.out.println(
        "Usage: java Exercise7_17 string");
      System.exit(0);
    }

    String s = args[0];

    if (isPalindrome(s)) {
      System.out.println(s + " is a palindrome");
    }
    else {
      System.out.println(s + " is not a palindrome");
    }
  }

  /** Check if a string is a palindrome */
  public static boolean isPalindrome(String s) {
    // The index of the first character in the string
    int low = 0;

    // The index of the last character in the string
    int high = s.length() - 1;

    while (low < high) {
      if (!equals(s.charAt(low), s.charAt(high)))
        return false; // Not a palindrome

      low++;
      high--;
    }

    return true; // The string is a palindrome
  }

  /** Return true if two letters are the same regardless case */
  public static boolean equals(char c1, char c2) {
    return lowercase(c1) == lowercase(c2);
  }

  public static char lowercase(char c) {
    if (c >= 'A' && c <= 'Z') {
      int offset = (int)'a' - (int)'A';
      char lowercase = (char)((int)c + offset);
      return lowercase;
    }
    else
      return c;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -