Results 1 to 5 of 5

Thread: How to extract Palindrome from a given sentence?

  1. #1
    Join Date
    Aug 2009
    Posts
    57

    How to extract Palindrome from a given sentence?

    Hello to all,
    I am new to this forum. I want to extract Palindrome from a given sentence or String? I tried various method but none of them worked out. Can anyone tell me how to Extract Palindrome from a given sentence? Please help me.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to extract Palindrome from a given sentence?

    You have to use the String.split() method to extract all the different words from the given sentence into an array and then for each word you have to check for a palindrome. Before doing this first make the string lowercase and to remove punctuation. Now try to execute following code.


    Code:
    String[] puncChars = {".", ",", "?", "!", ...}; 
    String input1 = "Hello sir, are you Indian ?";
    
    
    input1 = input1.toLowerCase();
    
    
    for (String punct : punc1Chars)
        input = input1.replaceAll(punct, "");
     
    
    String[] words1 = input1.split();

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to extract Palindrome from a given sentence?

    To extract Palindrome from a given sentence you have to use replaceAll() method for removing all punctuation from given sentence. replaceAll() process will remove all punctuation from given sentence and you will able to extract Palindrome easily from given sentence. Now you have to assign the result of the replacement back to input because given string is immutable. For this purpose you have to use following code.
    input1 = input1.replaceAll(punct, "");

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to extract Palindrome from a given sentence?

    You have to do following steps to extract Palindrome from a given sentence.
    1.You have to first read the input string or sentence.
    2.Now break down that sentence into words.
    3.Now extract second and second last word from given sentence.
    4.Now print these word in screen.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to extract Palindrome from a given sentence?

    Hey following program extract Palindrome from a given sentence. I have written that program for you. Just try to understand it. I have use for loop for accessing each character from given sentence. I also use BufferedReader for saving value for temporary. Just copy this code and paste it in your project and then try to run it.


    Code:
    import java.io.*;
    public class Palindrome {
         public static void main(String[] args) {
           try
            {
                int k=0,flag=0;
                String [] words = null;
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Enter the given string");
                String inputs = br.readLine();
                inputs=inputs.toLowerCase();
                words = inputs.split(" ");
                int lefts  = 0;                
                int rights = words[k].length() -1;  
                            for(k=0;k<d/2;k++)
                           {
                     while (lefts < rights)
                       {
                         if (words[k].charAt(lefts) == words[k].charAt(rights)) {
                         flag=1;
                         }
                         lefts++;
                         rights--;
                       }
                          }
    
                   
                     if(flags==1)
                        {
                        System.out.println( "palindrome words :"+wordss);
                        }
                        else
    
                        System.out.println("there is no plaindrome");
                   
           }
           catch(Exception e)
           {  System.out.println("Error");  }
    
         }
    }

Similar Threads

  1. Lets play the sentence game
    By jamesroy in forum Off Topic Chat
    Replies: 2
    Last Post: 06-02-2010, 05:31 PM
  2. C# program for palindrome
    By Smita.bendal in forum Software Development
    Replies: 5
    Last Post: 22-12-2009, 03:07 PM
  3. how to write program on palindrome in java?
    By Linoo in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 05:19 PM
  4. cannot change sentence in scanned text sheet
    By Bhuvan in forum Windows Software
    Replies: 3
    Last Post: 08-04-2009, 07:42 PM
  5. Program to determine a word or a phrase is a palindrome in C/C++
    By Lambard in forum Software Development
    Replies: 4
    Last Post: 09-12-2008, 07:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,034,706.77315 seconds with 17 queries