Skip to content

Programming by Design

If you're not prepared to be wrong, you'll never come up with anything original. – Sir Ken Robinson

  • About
  • Java-PbD
  • C-PbD
  • ASM-PbD
  • Algorithms
  • Other

Palindrome

Posted on October 29, 2024October 29, 2024 By William Jojo
Uncategorized
class Untitled {
    
    public static void genPal1() {
        for( int x = 0; x <301; x++) {
            StringBuffer sb;
            String s = String.format("%03d", x);
            sb = new StringBuffer(s).reverse();
            System.out.printf("%s%s%n", s, sb);
        }
    }
    
    public static void genPal2() {
        for (char one = '0'; one < '3'; one ++)
            for (char two = '0'; two <= '9'; two ++)
                for (char three = '0'; three <= '9'; three ++)
                    System.out.printf("%c%c%c%c%c%c%n", one, two, three, three, two, one);
    }
    
    public static boolean isPal(String s) {
        int b = 0, e = s.length()-1;
        while ( b < e) {
            System.out.println("b = " + b + " e = " + e);
            if (s.charAt(b) != s.charAt(e))
                return false;
            b++; e--;
        }
        return true;
    }
    
    public static void main(String[] args) {
        genPal2();
        System.out.println("1001001 " + isPal("1001001"));
        System.out.println("abba " + isPal("abba"));
        System.out.println("abca " + isPal("abca"));
        
    }
}

Post navigation

❮ Previous Post: Project 5 Showcase
Next Post: C64 BASIC Tokenizing ❯

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Copyright © 2018 – 2025 Programming by Design.