i am getting problem in this program plz help me..
Write a program to reverse each word in line(sentence)?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
i am getting problem in this program plz help me..
Write a program to reverse each word in line(sentence)?
What have you tried?
Where are you having problems?
List the steps the program needs to take to solve the problem and describe which step you are stuck on.
If you don't understand my answer, don't ignore it, ask a question.
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args)
{
Scanner scn=new Scanner(System.in);
System.out.println("write a sentence");
String s1=scn.nextLine();
String[] arr=s1.split("");
String s2="";
for(int i=arr.length-1;i>=0;i--)
{
s2+=arr[i]+"";
}
System.out.println(s2);
}
}
actually i am getting the below o/p-
o/p=write a sentence
hi how are you
uoy era woh ih
but i want this as a o/p --"ih woh era uoy"
can you help me??
--- Update ---
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args)
{
Scanner scn=new Scanner(System.in);
System.out.println("write a sentence");
String s1=scn.nextLine();
String[] arr=s1.split("");
String s2="";
for(int i=arr.length-1;i>=0;i--)
{
s2+=arr[i]+"";
}
System.out.println(s2);
}
}
actually i am getting the below o/p-
o/p=write a sentence
hi how are you
uoy era woh ih
but i want this as a o/p --"ih woh era uoy"
can you help me??
Please edit your post and wrap your code with code tags:
[code=java]
YOUR CODE GOES HERE
[/code]
to get highlighting and preserve formatting.
What does the program need to do to get that?but i want this as a o/p --"ih woh era uoy"
If you were going to explain the steps to a person, what would you tell them?
If you don't understand my answer, don't ignore it, ask a question.
i am getting problem in this program please help me..
Write a program to reverse each word in line(sentence)?
if i will give input as a
o/p would be --hi how are youih woh era uoy
well, split the string of words by space first, then you can loop through it and reverse the words in each loop.
You can use the string methods likeandsplit()reverse()
Threads merged.
If you don't understand my answer, don't ignore it, ask a question.
i got it thanks for your help everyone
import java.util.Scanner;
public class SentenceReverse
{
public static void main(String[] args)
{
Scanner scn = new Scanner(System.in);
System.out.println("Enter a string:");
String s = scn.nextLine();
String s1 ="";
String [] arr = s.split(" ");
for(int i=0;i<arr.length;i++)
{
s1 += new StringBuffer(arr[i]).reverse().toString() + " ";
}
System.out.println(s1);
}
}
Please edit your post and wrap your code with code tags:
[code=java]
YOUR CODE GOES HERE
[/code]
to get highlighting and preserve formatting.
If you don't understand my answer, don't ignore it, ask a question.