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:-
O/P:-i want to be a java developer
means swap first and last word and all alternate word.developer a be to java want
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 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:-
O/P:-i want to be a java developer
means swap first and last word and all alternate word.developer a be to java want
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.
yea that i have already done... but after that i am getting confused how to swap. can you write the looping code for 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.
first word would be swapped with last word and all the alternate word.
e.g
i/p-o/p-a b c d e ff 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??
That swap should be very easy.first word would be swapped with last word
That would take a loop with some indexes to keep track of what elements to swap.and all the alternate word
What have you tried?
If you don't understand my answer, don't ignore it, ask a question.
snehasishmohanta@gmail.co (July 8th, 2014)
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.
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.i am not getting the logic for swapping
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.
snehasishmohanta@gmail.co (July 8th, 2014)
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
will it work??temp=i[0];
i[0]=i[n-1];
i[n-1]=temp;
--- Update ---
thanks.. but what about the alternate word??
Look at the logic I suggested in post#8. Did you do the paper and pencil exercise?what about the alternate word??
How did you create and populate the array? That will determine the index to the last element in the array.how can i get the last element of that 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.
snehasishmohanta@gmail.co (July 8th, 2014)
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 .
Are you asking how to build the full String from the array's elements?converting that array element to string .
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.
snehasishmohanta@gmail.co (July 8th, 2014)
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.
Did you understand and work through my suggestions in post#8?how to swap alternate word on that string.
Using the variable names suggested in post#10 will help.
If you don't understand my answer, don't ignore it, ask a question.
that thing i am not getting... how to use the loop... please help me.
how to put index in a loop.
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.
it's looking more complicated . i am not getting what is 9 & 5.
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.
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.
You're welcome.
If you don't understand my answer, don't ignore it, ask a question.
sir , i faced a strange problem. i went for an interview. that interviewer asked me
i was unable to answer properly. any how i managed i answeredwhy we go for java instead of C and C++
due to flexible and user friendly and platform independent we are usingwhat more thing i would have say to him... help me...
Well, what differences between java and C++ do you know?
yes i know. but from my point of view some special differences are.
c++ support pointer where as java doesn't have pointer conceptif any more important thing there kindly share with me.in java it doesn't support diamond problem,here in C++ it supports