Palindrome ou non à partir d'un fichier

sikove Messages postés 90 Date d'inscription lundi 17 mars 2008 Statut Membre Dernière intervention 31 mars 2010 - 2 avril 2008 à 14:34
sikove Messages postés 90 Date d'inscription lundi 17 mars 2008 Statut Membre Dernière intervention 31 mars 2010 - 4 avril 2008 à 11:17
salut

voila je viens de créer un code qui permet de lire à partir d'un fichier et signaler si la phrase est palidrome ou non
:

import biputil.*;
import java.util.*;
public class entraine {
    public static void main(String args[])    {
        Vector tab=new Vector();
        SequentialFile fichier=new SequentialFile();
        fichier.open("ajout.txt",'r');
        String ligne=fichier.read();
        while(!fichier.EndOfFile)
        {
            tab.addElement(ligne);
            ligne=fichier.read();
        }
        fichier.close();
       
        int i,x,j,k,s,o;
        o=1;
        for (i=0; i<tab.size(); i++)
        {
            String mo=(String)tab.elementAt(i);
            char[] tab1=new char[50];       
                j=0;
                k=0;
            while (j<mo.length() && k<50)
            {
            tab1[k]=mo.charAt(j);
            j++;
            k++;
            }
            s=k-1;
            k=0;
            while (k<s && tab1[k]==tab1[s])
            {
                k++;
                s--;
            }
            if ( k==s || k==s+1) System.out.println(o+" cette ligne est palindrome:\t"+mo);
            else System.out.println(" non palindrome");
            o++;
        }   
            }
           
        }

j'aimerai bien savoir vos avis ^^

2 réponses

Ombitious_Developper Messages postés 2333 Date d'inscription samedi 28 février 2004 Statut Membre Dernière intervention 26 juillet 2013 38
2 avril 2008 à 15:53
Salut:

public static boolean isPalindrome(String text) {
    if (text == null) {
       throw new NullPointerException();
    }
    return new StringBuffer(text).reverse().toString().equals(text);
}
0
sikove Messages postés 90 Date d'inscription lundi 17 mars 2008 Statut Membre Dernière intervention 31 mars 2010
4 avril 2008 à 11:17
merci, c'est plus pratique
0
Rejoignez-nous