Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Finding out number of Palindromes in a Sentence

  1. #1
    Member
    Join Date
    Sep 2010
    Posts
    32
    My Mood
    Confused
    Thanks
    9
    Thanked 0 Times in 0 Posts

    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());
    }
     
     
    }
     
    }
    Last edited by copeg; September 12th, 2010 at 10:19 PM. Reason: Code tags


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Finding out number of Palindromes in a Sentence

    Ok, well for removing "special characters" you need to first figure out what "special characters" are. Then you could use the String.replace(char,char) method to "remove" them.

    For reversing the word, you can convert the String to a char[] by using the method String.toCharArray(), then rebuild the String by going through the array in reverse.

    Have a look at the String API: String (Java Platform SE 6)

Similar Threads

  1. Finding the highest number in an array?
    By halfwaygone in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 17th, 2010, 03:56 PM
  2. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM
  3. [SOLVED] Recursive Sentence Finder
    By raphytaffy in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 21st, 2010, 02:46 PM
  4. making a sentence proper
    By dvsumosize in forum Algorithms & Recursion
    Replies: 5
    Last Post: February 3rd, 2010, 11:01 AM
  5. reading a sentence
    By lotus in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: July 20th, 2009, 08:29 AM