Results 1 to 2 of 2
- 09-13-2010, 03:37 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 12
- Rep Power
- 0
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());
}
}
}
- 09-13-2010, 03:44 AM #2
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 03:47 AM.
Similar Threads
-
Finding a word in a sentence?
By blackrabbit in forum New To JavaReplies: 6Last Post: 07-22-2010, 11:07 PM -
Finding nth prime number
By dextr in forum New To JavaReplies: 2Last Post: 04-12-2010, 11:42 PM -
Finding a number in array close to another number
By SteroidalPsycho in forum New To JavaReplies: 2Last Post: 02-15-2010, 12:37 AM -
finding length on a number
By thekrazykid in forum New To JavaReplies: 8Last Post: 12-12-2008, 08:07 PM -
Finding the highest number
By jigglywiggly in forum New To JavaReplies: 7Last Post: 11-04-2008, 08:14 AM


LinkBack URL
About LinkBacks

Bookmarks