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 5 of 5

Thread: Reverse Words Problem

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reverse Words Problem

    Hi, i am trying to create a method that will accept a string, it will then return a string with every pair of words in the first string reversed.

    For example, input string: " Hello this is a test string to reverse every pair of words "
    output string: " this Hello a is string test reverse to pair every words of"

    so far i wrote the code below but i am getting out of bounds errors and can't get it to work. can someone please help me and/or suggest the best way to do this..I really appreciate it. Thanks.

    //

    public static String wordPairReverse(String source){

    StringTokenizer st = new StringTokenizer(source, " ");
    String strReversedLine = "";
    String tempString = "";
    String pairs[]= new String[20];
    char j=0;
    char i=0;

    while(st.hasMoreTokens()){

    while(j<2){

    tempString=st.nextToken()+" "+tempString;
    j++;
    }
    j=0;
    pairs[i]=tempString;
    i++;
    }

    while(i>0){
    strReversedLine = pairs[i] + " " + strReversedLine;
    i--;
    }

    return strReversedLine;

    }
    //


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: Reverse Words Problem

    Take this in smaller sections. Write a function that takes a single word and returns its reverse. Write another function that takes a String of multiple words and returns an array or an ArrayList of single words. Write yet another function that takes an array or ArrayList of single words and loops through them, calling the reverse function on each.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reverse Words Problem

    Please use a Stack collection for your task



    __________________________________________________ ________________________
    How to make a flash game?

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Location
    Delhi
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reverse Words Problem

    ...edited by moderator...
    Last edited by copeg; October 16th, 2011 at 04:03 PM.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,337
    Thanks
    182
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Reverse Words Problem

    Saurabh Mittal, please read the forum rules and the following link:
    http://www.javaprogrammingforums.com...n-feeding.html

Similar Threads

  1. Reverse every pair of words in a string
    By mulligan252 in forum Collections and Generics
    Replies: 4
    Last Post: October 11th, 2011, 04:29 PM
  2. Replies: 2
    Last Post: September 8th, 2011, 09:50 PM
  3. Reverse String
    By WantHelp in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 5th, 2011, 06:14 AM
  4. Help: Num to words
    By shamed in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 7th, 2010, 05:55 PM
  5. Reverse Number
    By java1 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 28th, 2009, 10:19 AM