Results 1 to 2 of 2
  1. #1
    rameshiit19 is offline Member
    Join Date
    Sep 2010
    Posts
    12
    Rep Power
    0

    Question Finding out number of Palindromes in a Sentence

    I have tried to do a program to find number of palindromes in a sentence but couldn't complete it since I doesn't know to reverse the string and also to remove special characters. Please help!

    My code:

    import java.io.*;
    import java.lang.*;
    class palindrome1
    {
    public static void main(String args[])
    {
    System.out.println("Welcome");
    getsentence();
    }


    public static void getsentence()
    {
    try
    {
    String sent=" ";
    String s1=" ";
    String[] punct={".",",","?","!"};
    String input=" ";
    String[] temp;
    String[] temp1={ } ;
    String[] revstrng;
    int i=0;
    String delimiter=" ";
    int len=0;

    System.out.println("Enter the Sentence to find Palindrome Count:");

    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    sent=in.readLine();
    s1=sent.toLowerCase();
    System.out.println("The entered sentence is:" + s1);

    temp=s1.split(delimiter);
    for(int j=0;i<temp.length;i++)
    {
    System.out.println(temp[i]);

    if(temp1[i]==(temp[i].reverse()))

    {
    i++;
    }
    System.out.println("Number of Palindromes in Sentence:" + i);
    }



    }
    catch(Exception e)
    {
    System.out.println(e.getMessage());
    }


    }

    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    A reverse method works pretty simply:
    1) Start with the function declaration--it should accept a single String as a parameter.
    2) Begin with an empty String.
    3) Loop through your string (letter for letter), then add each letter to the empty String in reverse order. (That is, if you're looping forward, add each letter to the end of the empty String.)
    4) Return the new string.

    To check if something is not a letter or number, you can use Character.isLetterOrDigit as well as the other functions in that class to broaden your search (i.e. check for unicode, !@#$%^&*()?"', and so on).

    PS: This method will interest you.
    PPS: When comparing strings, use .equals() or .compareTo() as == compares the refences. That is, two separate String objects will never equate using == even if they contain the same characters.
    Last edited by Zack; 09-13-2010 at 04:47 AM.

Similar Threads

  1. Finding a word in a sentence?
    By blackrabbit in forum New To Java
    Replies: 6
    Last Post: 07-23-2010, 12:07 AM
  2. Finding nth prime number
    By dextr in forum New To Java
    Replies: 2
    Last Post: 04-13-2010, 12:42 AM
  3. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 01:37 AM
  4. finding length on a number
    By thekrazykid in forum New To Java
    Replies: 8
    Last Post: 12-12-2008, 09:07 PM
  5. Finding the highest number
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 11-04-2008, 09:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •