Capicua java character string

3

I am trying to make a program that says if the introduced phrase or word is capicua, that is to say holaloh. What I do is two for, which starts to run from the beginning and the other from the end, and then save them with the charAt and the position of the index but I do well in some words and not in others. I do not know if there's something wrong with the code.

import java.util.Scanner;

public class capicua {
public static void main(String args[]) {
    Scanner teclado = new Scanner(System.in);

    System.out.println("Escriba una cadena.");
    String x = teclado.nextLine();
    int s = x.length();
    int z = s / 2;
    char k;
    char m;

    for (int i = 0; i <= z; i++) {
        for (int j = (s - 1); j>z ; j--) {

            k = x.charAt(i);
            m = x.charAt(j);

            if (k==m) {
                System.out.println("Bien.");
            }

        }
     }
  }
}
    
asked by Fernando 22.01.2017 в 16:38
source

2 answers

3

It is not necessary that user for nested, you were not comparing what should be the comparison for this problem, which is to compare

  

the first element with the last

     

the second element with the penultimate

     

the third element with the second-to-last   etc.

your error lies in the comparison: what your code does is:

  

i = 0
  compare the first element with the last element

     

compare the first element with the penultimate element

     

compare the first element with the second element ...

     

i = 1
  compare the second element with the last element

     

compare the second element with the penultimate element

     

compare the second element with the second element ...

     

i = 2

     

compare the third element with the last element

     

compare the third element with the penultimate element

     

compare the third element with the third element ... etc

I leave you a solution:

import java.util.Scanner;

public class capicua {
public static void main(String args[]) {
    Scanner teclado = new Scanner(System.in);

    String x = "anitalavalatina";
    int s = x.length();
    int z = s / 2;
    char k;
    char m;
    int p=0;

        for (int j = (s - 1); j>z && p<z ; j--) {

            k = x.charAt(p);
            m = x.charAt(j);

      System.out.println(k+" "+m );   

            if (k==m) {
                System.out.println("Bien.");
            }
          p++;

        }

  }
}
    
answered by 22.01.2017 / 16:48
source
0

If you do not have limitations to use StringUtils the code will be much smaller:

String prueba = "prueba";
String aux = StringUtils.reverse(prueba);
System.out.println(String.format("'%s' %s es palíndromo: '%s'", prueba, (prueba.equals(aux)?"SI":"NO"),aux));

prueba = "acurruca";
aux = StringUtils.reverse(prueba);
System.out.println(String.format("'%s' %s es palíndromo: '%s'", prueba, (prueba.equals(aux)?"SI":"NO"),aux));

It gives us the following output:

  

'test' is NOT palindrome: 'abeurp'
  'curl up' IF it's palindrome: 'snuggle up'

NOTE: When we refer to words the expression palindrome is used for what you are looking for instead of capicúa that is used for numerals.

    
answered by 23.01.2017 в 14:46