// A method for finding the longest and shortest string
//and a method to split each word so i can see if it is a
// palindrome or not thanks for the help




{
    public static void main(String[] args)
    {
        String string ;
        int index ;
        String  sentence;
        String  newSentence; 
        String  word;
        int startPos;
        int spacePos;
        int wordCount;
        boolean lastWord;
        boolean alphaWord;
        char letter;


        System.out.println("Enter a string");
        sentence = EasyIn.getString();

        startPos = 0;
        wordCount = 0;
        lastWord = false;
        newSentence = "";
        do
        {
            spacePos = sentence.indexOf(" ", startPos);
            if (spacePos == -1)
            {
                lastWord = true;
                word = sentence.substring(startPos);
            }
            else
            {
                lastWord = false;
                word = sentence.substring(startPos, spacePos);
            }
            index = 0;
            alphaWord = true;
            while ((index <= word.length()-1) && (alphaWord == true))
            {
                letter = word.charAt(index);
                if ((letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z'))
                    index++;
                else
                    alphaWord  = false;
            }
            if (alphaWord == true)
                {
                    newSentence = newSentence + word + " ";     
                }
            startPos = spacePos + 1;
        } while (lastWord != true)
        System.out.println();
        for(index = newSentence.length()-1 ;index >= 0 ; index--)
        {
        System.out.print(" "+  newSentence.charAt(index) );
        }





    }

 }

Edited 3 Years Ago by Nick Evan: Fixed formatting

I suppose you are allowed to use the String class' methods.
1. For longest & shortest word use length method of String.
2. To split use StringTokenizer class
3. For palindrome make use of the stack ds.
If you cannot use String methods you'll have to traverse the string maintaining a counter until you encounter a termination char like a space, comma, period or newline.

This article has been dead for over six months. Start a new discussion instead.