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.

Results 1 to 9 of 9

Thread: Write a program to reverse each word in line(sentence)?

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    22
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Unhappy Write a program to reverse each word in line(sentence)?

    i am getting problem in this program plz help me..
    Write a program to reverse each word in line(sentence)?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,098
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: 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.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    22
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Write a program to reverse each word in line(sentence)?

    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??

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,098
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Write a program to reverse each word in line(sentence)?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    but i want this as a o/p --"ih woh era uoy"
    What does the program need to do to get that?
    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.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    22
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Write a program to reverse each word in line(sentence)?

    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
    hi how are you
    o/p would be --
     ih woh era uoy

  6. #6
    Junior Member codepoet's Avatar
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write a program to reverse each word in line(sentence)?

    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 like
    split()
    and
    reverse()

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,098
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Write a program to reverse each word in line(sentence)?

    Threads merged.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jul 2014
    Posts
    22
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Write a program to reverse each word in line(sentence)?

    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);
    }
    }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,098
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Write a program to reverse each word in line(sentence)?

    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.

Similar Threads

  1. Write a program to convert all vowels to capital letter in a sentence?
    By snehasishmohanta@gmail.co in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 3rd, 2014, 09:33 AM
  2. Replies: 1
    Last Post: November 8th, 2013, 07:31 AM
  3. Print a given sentence forwards, backwards and every other word.
    By newbiegirl in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 24th, 2013, 05:45 AM
  4. reverse line feed using java printing
    By ranjith. in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 20th, 2013, 09:17 AM
  5. Method to take in a string sentence and reverse the tokens
    By ksahakian21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 25th, 2012, 05:11 PM