Java program to find number of pallindromes words in a string and prin

i cannot use split function plx help me correct the errors

Page 1 of 1

8 Replies - 2194 Views - Last Post: 18 January 2009 - 04:54 PM Rate Topic: -----

#1 priya_lorha  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 78
  • Joined: 02-August 08

Java program to find number of pallindromes words in a string and prin

Post icon  Posted 18 January 2009 - 06:51 AM

[\code]class palin
{public static void main(String args[])
{ int i,j,k,l,lp,counter=0;
String m="my mom chocolate cake icecream arora madam dog coco kaka itat papa",n="",o,p="";
char ch=' ',ph=' ',bh=' ',kh=' ';
l=m.length();
// loop
for(i=0;i<l;i++)
{ch=m.charAt(i);
if(ch==' '){break;}
p=m.substring(0,i);
continue;}
lp=p.length();
for(j=lp-1;j>=0;j--)
{ch=p.charAt(j);
n=n+ch;System.out.println(n);
}if(n.equalsIgnoreCase(p));
{System.out.println("The word is pallindrome");
System.out.println(p);
counter++;
}System.out.println("nos of pallindrome words="+counter);}
}





[\code]

This post has been edited by priya_lorha: 18 January 2009 - 06:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Java program to find number of pallindromes words in a string and prin

#2 priya_lorha  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 78
  • Joined: 02-August 08

Re: Java program to find number of pallindromes words in a string and prin

Posted 18 January 2009 - 06:54 AM

[code]class palin
{public static void main(String args[])
{ int i,j,k,l,lp,counter=0;
String m="my mom chocolate cake icecream arora madam dog coco kaka itat papa",n="",o,p="";
char ch=' ',ph=' ',bh=' ',kh=' ';
l=m.length();
// loop
for(i=0;i<l;i++)
{ch=m.charAt(i);
if(ch==' '){break;}
p=m.substring(0,i);
continue;}
lp=p.length();
for(j=lp-1;j>=0;j--)
{ch=p.charAt(j);
n=n+ch;System.out.println(n);
}if(n.equalsIgnoreCase(p));
{System.out.println("The word is pallindrome");
System.out.println(p);
counter++;
}System.out.println("nos of pallindrome words="+counter);}
}





[\code]
Was This Post Helpful? 0
  • +
  • -

#3 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Java program to find number of pallindromes words in a string and prin

Posted 18 January 2009 - 06:57 AM

Please don't double post and try to fix your code tags. The end tag is supposed to be [ /code] (without the blank), not [\code]
Take care of that and please state your problem and I'll try to help you out.

This post has been edited by Gloin: 18 January 2009 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Java program to find number of pallindromes words in a string and prin

Posted 18 January 2009 - 06:59 AM

And please, your code is very hard to read which just makes it harder for people to help you. Use proper indentation to help yourself and us.
Was This Post Helpful? 0
  • +
  • -

#5 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Java program to find number of pallindromes words in a string and prin

Posted 18 January 2009 - 07:07 AM

The indentation is usually lost when you don't have the code tags since a blank can hardly be seen.
Was This Post Helpful? 0
  • +
  • -

#6 Cup_of_java  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 30-October 08

Re: Java program to find number of pallindromes words in a string and prin

Posted 18 January 2009 - 08:33 AM

do you know the difference between:

word=word+newWord;


and

word=newWord+word?


create a new word that is the pallindrome of the word you're checking, then compare the two, if they equal, counter++;
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Java program to find number of pallindromes words in a string and prin

Posted 18 January 2009 - 09:16 AM

I had to do this assignment for my programming class. Here's my logic: First, create 2 ArrayLists; one to hold the letters in the string going from 0 to length-1, and the second to hold the letters in the string in reverse order. From there just compare the characters in the lists. If two characters at the same index don't match, then the string isn't a palindrome. The method I used is the Character.isLetter() method.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java program to find number of pallindromes words in a string and prin

Posted 18 January 2009 - 01:29 PM

Why re-inventing the wheel ?
Use a StringTokenizer !!!


StringTokenizer st = new StringTokenizer(m);
while(st.hasNext()) {
   String nextWord = st.next();
}



Duplicate topics: topics merged
Was This Post Helpful? 0
  • +
  • -

#9 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Java program to find number of pallindromes words in a string and prin

Posted 18 January 2009 - 04:54 PM

i agree with pbl, why reinvent the wheel, but since you dont want to use the split method or tokenizer and you wanna do the splitting yourself then.
import java.util.Scanner;
public class NumberOfPalindromes
{
	public static void main(String[]args)
	{
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		String x = "";
		int i = 0;
		while(i < s.length())
		{
			x = "";
			if(s.charAt(i) == ' ')
				i++;
			do
			{
				x += s.charAt(i);
				i++;
			}
			while(i < s.length() && s.charAt(i) != ' ');		
		}
		
	}
}


this algorithm will assign word by word(with spaces as delimiter) to String x on each iteration, then you can pass x to another method which checks for palindromes, also you will need to add an if statement and a counter variable to count the number of palindromes...
note though that when i wrote this algorithm i assumed that the user will enter the string with exactly one space between words, i didnt take into account if the user entered something like("welcome to dic ") only things like ("welcome to dic") with a single space between words..anyway i hope this helps and good luck

This post has been edited by mostyfriedman: 18 January 2009 - 04:55 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1