Not getting the desired output and don't know whats wrong with it


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Not getting the desired output and don't know whats wrong with it

  1. #1
    Join Date
    Mar 2010
    Location
    Chandigarh
    Posts
    4

    Not getting the desired output and don't know whats wrong with it

    Please check the following program and do let me know what is the problem with it. I'm not getting the desired output for it.

    The program is to show that the string entered is palindrom or not. Initially I was trying to do it for a particular string and later on when the problem is resolved i'll take input from user. I know there are other methods to do it but what is wrong with it.

    Code:
    class Palin
    {
    	public static void main(String arg[])
    	{
    
    		char a[]= new char[10];
    
    		char c[]={'a','d','a'};
    
    		String s1 = new String(c);
    		int l;
    
    		l=s1.length();
    
    		System.out.println("String is "+s1);
    
    		for(int i=0,j=l-1; i<l;i++,j--)
    		{
    			a[i]=c[j];
    		}
    
    		String s2 = new String(a);
    		System.out.println("String after reversing is "+ s2);
    
    		if(s1.equals(s2)==true)
    		System.out.println("Its a Palindrom");
    
    		else
    		System.out.println("Its not a Palindrom");
    	}
    }

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Does this help?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Mar 2010
    Location
    Chandigarh
    Posts
    4
    Thanks for replying.

    I know there are other ways to do but I just want to know that what's wrong with this one.

  4. #4
    Join Date
    Jan 2010
    Posts
    1

    Post Problem with Character declaration

    Hi shruti,
    The problem with your code is your character array initialization.
    char a[]= new char[10]; Since your character size is three, rest all characters shall be filled with spaces(not sure but its size wld be 10). So please change the size to 3 or else better way would be to initialize as follows:
    char a[] = new char[iSize]; where iSize wld be the size of input array.

  5. #5
    Join Date
    Apr 2010
    Posts
    1
    Code:
    char c[]={'a','d','a'};
    		char a[]= new char[c.length];
    		String s1 = new String(c);
    		int l;
     		l=s1.length();
    		System.out.println("String is "+s1);
    		for(int i=0,j=l-1; i<l;i++,j--)
    		{
    			a[i]=c[j];
    		}
    		String s2 = new String(a);
    		System.out.println("String after reversing is "+ s2);
    		if(s1.equals(s2)==true)
    		System.out.println("Its a Palindrom");
    		else
    		System.out.println("Its not a Palindrom");[/SIZE][/SIZE][/SIZE]
    Last edited by Hack; 04-27-2010 at 12:02 PM.

  6. #6
    Join Date
    Apr 2010
    Posts
    1

    answer

    Code:
    //char a[]= new char[10];
    
    		char c[]={'a','d','a'};
    		char a[]= new char[c.length];
    Last edited by Hack; 04-27-2010 at 12:02 PM. Reason: Added Code Tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center