-3

I've been trying to do this question for a while. It goes as follows:

Given a string S containing a few words. If the count of words in the string S is even then reverse its even position’s words else reverse its odd position, push reversed words at the starting of a new string and append the remaining words as it is in order. For example:

Input string: Ashish Yadav Abhishek Rajput Sunil Pundir

Output string: ridnuP tupjaR vadaY Ashish Abhishek Sunil

Do we have to take a string input from user and then convert it into an array and then split it from the middle...? I am very confused. Please give advice.

ílaya
  • 53
  • 1
  • 6
  • 1
    Maybe you can take the string as a command line parameter. Maybe you just need to write a function which takes a string parameter. Ask your teacher. – tgdavies Sep 06 '22 at 11:39
  • cannot use command line for this question! its on a coding labs site! @tgdavies – ílaya Sep 06 '22 at 11:51
  • In which language your trying to this? and you are question is about how to get the input or how to write program for this? – Prabaharan Balaji Sep 06 '22 at 12:01

1 Answers1

2

I'm not quite sure: is this a task that has been given to you by your teacher? Assuming this (because it seems so), I don't provide any actual code snippets, just an idea and you try to solve it at your own. If this is a real task: do not hesitate to ask for a solution but I think with the following context you will be able to do it at your own ; )

  1. How you get the string shouldn't matter. For a first test, just hardcode it.
  2. Split the string at each space (How to) and safe it in an array.
  3. As your array has indexes: use them to check whether it is even or odd (modulo 2) and safe the even- and odd words in extra arrays.
  4. From there on you just have to reverse every character in one of your arrays (In a loop or with an existing function (recommended))
  5. Now create a string. Fill it according to your instructions (so the reversed strings start at the highest index and decrement. Then add the non-reversed strings, starting at the lowest index and increment).
Simon
  • 19
  • 4