(Coderbyte) Palindrome Two - Solução

Have the function PalindromeTwo(str) take the str parameter being passed and return the string true if the parameter is a palindrome, (the string is the same forward as it is backward) otherwise return the string false. The parameter entered may have punctuation and symbols but they should not affect whether the string is in fact a palindrome. For example: "Anne, I vote more cars race Rome-to-Vienna" should return true.

Solução:

import java.util.*;
import java.io.*;

class Function { 
  String PalindromeTwo(String str) {
      str = str.toLowerCase();
   
    String apenasLetras = "";
    for (int i = 0; i < str.length(); i++) {
      if (Character.isLetter(str.charAt(i))) {
        apenasLetras += str.charAt(i);
      }
    }
   
    int tam = apenasLetras.length()/2;
    for (int i = 0, j = apenasLetras.length()-1; i < tam; i++, j--) {
      if (apenasLetras.charAt(i) != apenasLetras.charAt(j)) {
        return "false";
      }
    }
      
    return "true";
   
  }
 
  public static void main (String[] args) { 
    // keep this function call here    
    Scanner  s = new Scanner(System.in);
    Function c = new Function();
    System.out.print(c.PalindromeTwo(s.nextLine()));
  }  
 
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução