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

Thread: swapping.

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

    Unhappy swapping.

    i need a help...
    i just need the logic..

    i want to swap the word. how could i be able to swap..

    for example:-
    I/P:-
    i want to be a java developer
    O/P:-
    developer a be to java want
    means swap first and last word and all alternate word.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: swapping.

    Put the words in an array where swapping them will be easy.
    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: swapping.

    yea that i have already done... but after that i am getting confused how to swap. can you write the looping code for swapping.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: swapping.

    Can you explain what words are supposed to be swapped?
    Why do you think there needs to be a loop to do the swapping? A loop would be used if all the words need to be moved around. If there are only a few to be swapped, then a loop would not be needed.
    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 Re: swapping.

    first word would be swapped with last word and all the alternate word.
    e.g
    i/p-
    a b c d e f
    o/p-
    f b d c e a

    there are 6 word:- 1st place will be swapped with 6th position
    then 2nd place will not be swapped.
    then 3rd placed will swapped with 5th position

    --- Update ---

    it's getting swapped with a flow that is why i thought if we will use for loop it will save a lot of time. you got what i supposed to explain on the above post??

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: swapping.

    first word would be swapped with last word
    That swap should be very easy.

    and all the alternate word
    That would take a loop with some indexes to keep track of what elements to swap.

    What have you tried?
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    snehasishmohanta@gmail.co (July 8th, 2014)

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

    Default Re: swapping.

    actually i am not getting the logic for swapping.... actually the question was a big question and its the small part in which i am getting problem.
    i solved all the part.but stuck on this part. so i asked for the help.
    please help me.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: swapping.

    i am not getting the logic for swapping
    To work out the steps the code must take to solve the problem, take a piece of paper and write on a line a sample list of words. Then put pointers under the first words to be swapped(the first one and the last one). Do the steps to swap those words and then move the pointers to the next pair of words to swap. Swap them. Continue until all swaps have been done.
    Now look at how the values of the pointers were initially set and how their values were changed and how the end of the swap was detected.

    How to swap values in two variables: X and Y
    temp = X // save X
    X = Y
    Y = temp // put original value of X in Y
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    snehasishmohanta@gmail.co (July 8th, 2014)

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

    Default Re: swapping.

    if i will get the i/p through scanner. how can i get the last element of that array
    let the array name is i
    if the length of the array is n
    temp=i[0];
    i[0]=i[n-1];
    i[n-1]=temp;
    will it work??

    --- Update ---

    thanks.. but what about the alternate word??

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: swapping.

    what about the alternate word??
    Look at the logic I suggested in post#8. Did you do the paper and pencil exercise?

    how can i get the last element of that array
    How did you create and populate the array? That will determine the index to the last element in the array.

    The code will be easier to understand if you use meaningful names for the variables:
    words for the array that holds the words
    firstIndex for the index to the left hand element
    lastIndex for the index to the right hand element
    The swaps will be of the words array's elements at firstIndex and lastIndex
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    snehasishmohanta@gmail.co (July 8th, 2014)

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

    Default Re: swapping.

    yea i tried. but after splitting the string i stored in the array and then swapped the first element with the last element.after that i got confused again for converting that array element to string .

  15. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: swapping.

    converting that array element to string .
    Are you asking how to build the full String from the array's elements?
    Use the concatenation operator: + in a loop to concatenate all the elements to a String.
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    snehasishmohanta@gmail.co (July 8th, 2014)

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

    Default Re: swapping.

    sorry for the silly question. actually i am coding from morning now it's 8.00 pm. i think codes are fighting on my head. i think i need little free time to brush up my mind. sorry to take your valuable time for my silly question. hope you don't mind.. and thanks for helping me always on my problem time

    --- Update ---

    but your task is not finished yet. you have to help me how to swap alternate word on that string.

  18. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: swapping.

    how to swap alternate word on that string.
    Did you understand and work through my suggestions in post#8?
    Using the variable names suggested in post#10 will help.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: swapping.

    that thing i am not getting... how to use the loop... please help me.
    how to put index in a loop.

  20. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: swapping.

    The for loop has a built in index usage feature. You can then manually write code that executes inside the loop for any other indexes you need.

    Alternately the for loop can handle multiple indexes.
    This code has 2 indexes: i & j:
          for(int i=0, j=9; i < j; i++, j = j - 5){
             System.out.println("i="+i +", j="+j);
          }
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: swapping.

    it's looking more complicated . i am not getting what is 9 & 5.

  22. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: swapping.

    That is an example of a for loop that controls two indexes. It is only an example.
    Did you copy it to a test class, compile and execute it to see how the values of i and j change? If that is too complicated, use the other technique of manually incrementing the indexes.

    Review the syntax of a for loop:
    http://docs.oracle.com/javase/tutori...bolts/for.html
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: swapping.

    i think you are saying about the for loop. yes i got that.

    --- Update ---

    you are looking very helpful person. can i have your facebook profile???

    --- Update ---

    many many thanks to you.. for increasing my knowledge.

  24. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: swapping.

    You're welcome.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: swapping.

    sir , i faced a strange problem. i went for an interview. that interviewer asked me

    why we go for java instead of C and C++
    i was unable to answer properly. any how i managed i answered
    due to flexible and user friendly and platform independent we are using
    what more thing i would have say to him... help me...

  26. #22
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: swapping.

    Well, what differences between java and C++ do you know?

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

    Default Re: swapping.

    yes i know. but from my point of view some special differences are.
    c++ support pointer where as java doesn't have pointer concept
    in java it doesn't support diamond problem,here in C++ it supports
    if any more important thing there kindly share with me.

Similar Threads

  1. Replies: 7
    Last Post: November 4th, 2013, 03:07 AM
  2. swapping integers OOP
    By iamgonge in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2013, 06:19 AM
  3. swapping nodes in a linked list
    By ueg1990 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 10th, 2012, 02:37 PM