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 every pair of words in a string

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

    Default Reverse every pair of words in a string

    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 have below but i am stuck and really need help. I really appreciate it. Thanks
    //
    public static String wordPairReverse(String source){
     
    		StringTokenizer st = new StringTokenizer(source, " ");
    		String strReversedLine = "";
    		char j=0;
     
    		while(st.hasMoreTokens()){
     
    			while(j<2){
     
    			strReversedLine=st.nextToken()+" "+strReversedLine;
    			j++;
     
     
     
    		...
     
    	}
    //


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Reverse every pair of words in a string

    No need to use StringTokenizer etc.
    All this can be done as 1 line inside a for loop.

    Play around with a for loop approach, using String.spit() to your advantage.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    Default Re: Reverse every pair of words in a string

    I tried this, but i am getting all sorts of errors. Could someone please suggest a better way if possible. 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;

    }
    //

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Reverse every pair of words in a string

    I tried this, but i am getting all sorts of errors.
    What errors? Post them in their entirety, (and I recommend the get help link in my signature). How would you do this by hand? Write it out on a piece of paper - this often helps wrap ones head around the problem, which then help to lay out the algorithm in code.

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

    Default Re: Reverse every pair of words in a string

    Its telling me out of bounds exception. I have been trying hours now to get this code working but no success. Can someone please suggest the way i should do it please.

Similar Threads

  1. Replies: 2
    Last Post: September 8th, 2011, 09:50 PM
  2. Reverse String
    By WantHelp in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 5th, 2011, 06:14 AM
  3. How to reverse a string, skiping numbers , and ' ?
    By mlotfi in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 26th, 2011, 05:13 AM
  4. How to reverse a String using java.lang.StringBuilder
    By JavaPF in forum Java SE API Tutorials
    Replies: 0
    Last Post: July 22nd, 2009, 09:42 AM
  5. How to reverse a String using java.lang.StringBuilder
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: July 22nd, 2009, 09:42 AM