- #1
- 95
- 0
We are asked to reverse a sentence , example: I am going. ==> .going am I
Attempt:
import java.util.Scanner;
public class Assign2
{
public static void main (String [] args)
{
String line= new StringBuffer().reverse().toString();
String line2 = new StringBuffer(line).reverse().toString();
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter a line of text. No punctuation please. ");
line=keyboard.nextLine();
System.out.println( "I have rephrased that line to read ");
System.out.println( new StringBuffer(line).reverse().toString() );
}
}
Output:
Enter a line of text. No punctuation please.
Hello I am
I have rephrased that line to read
ma I olleH
Is there a way to do this problem without using loop?
Can someone give me a head start on how to do it using substrings?
Please, try to lay out some steps as I am trying to understand it. Thanks.
Attempt:
import java.util.Scanner;
public class Assign2
{
public static void main (String [] args)
{
String line= new StringBuffer().reverse().toString();
String line2 = new StringBuffer(line).reverse().toString();
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter a line of text. No punctuation please. ");
line=keyboard.nextLine();
System.out.println( "I have rephrased that line to read ");
System.out.println( new StringBuffer(line).reverse().toString() );
}
}
Output:
Enter a line of text. No punctuation please.
Hello I am
I have rephrased that line to read
ma I olleH
Is there a way to do this problem without using loop?
Can someone give me a head start on how to do it using substrings?
Please, try to lay out some steps as I am trying to understand it. Thanks.