(Coderbyte) Palindrome - Solução

Have the function Palindrome(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. For example: "racecar" is also "racecar" backwards. Punctuation and numbers will not be part of the string.

Solução:

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

class Function { 
  String Palindrome(String str) {
 
    str = str.replaceAll(" ", "");
      
    int tam = str.length()/2;
   
    for (int i = 0, j = str.length()-1; i < tam; i++, j--) {
      if (str.charAt(i) != str.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.Palindrome(s.nextLine()));
  }  
 
}          

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Powers of Two - Solução

(CoderByte) Number Search - Solução