• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

demontrate reverse string in java

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write, compile, and run a Java program that: (1) uses a single line comment to
document your source code with your name and section number. This should be the
very first line of your program, (2) prints your name and section number as the first line
of your output, (3) reads a string from the key board and stores it in a string variable
named “s1”, (4) uses iteration to reverse the string and store the reversed string in “s2",
(5) compares “s1” to “s2” to determine if the string is a palindrome, and (6) prints the
input string, the input string without blank spaces, the output string, and a statement
whether or not the input string is a palindrome or not.
here is my code
import java.util.*;
import java.util.Scanner;
public class LabFour
{
public static void main(String[] args)
{
print_name();
Scanner kybd = new Scanner(System.in);
String s1, s2="";
System.out.println("Enter a string ");
s1 = kybd.nextLine();
int length = s1.length();
s1= s1.replaceAll("\\.","");
System.out.println(" string to input: "+s1);
for( int i= length - 1; i>=0; i--)
{

s2 = s2 + s1.charAt(i);


}
System.out.println("Reversed string is "+s2);

if(s1.equals(s2))

{

System.out.println ("its a pallindrome");

}

else

{

System.out.println("not a pallindrome");
}
}
public static void print_name()
{
System.out.println("Asong A Defang LD01");
}// end printname

}// end LabFour
i dont know how to make my code replace all spaces , an also when i do the reverse its still prmpting "its not a pallindrome" . i need help please
 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You program works really well. In order to remove spaces, use String's replace method. It also fails on "Hannah". I think you can figure out why, but if you need any help, feel free to post back.
 
atem frank
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Christophel wrote:You program works really well. In order to remove spaces, use String's replace method. It also fails on "Hannah". I think you can figure out why, but if you need any help, feel free to post back.


thanks so much, i finally got it
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic