Reorder Sentence in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • culture
    New Member
    • Oct 2008
    • 5

    Reorder Sentence in C++

    Hello,
    I have a problem in C++ that has been giving me a bit of problem.

    I am suppose to write a program that allows a user to enter a sentence, and then ask for two positions from the user, and then switch around the sentence according to the positions entered.

    If the user enters a negative number as a position to switch; the program should end.

    If the user enters a number less than one, or greater than the number of words in the sentence, the following should print "Invalid position entered"
    and ask for the first position again.

    For example a user typed "The cat sat on mat the"
    The program would look like this:

    Enter the sentence: The cat sat on mat the

    Enter first position: 5
    Enter second position: 6

    The cat sat on the mat

    Please can someone assist me as soon as possible. Thank you
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    What have you done so far?

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      Originally posted by culture
      I am suppose to write a program that allows a user to enter a sentence, and then ask for two positions from the user, and then switch around the sentence according to the positions entered.
      if you enter m and n, should it reverse the whole [m,n] block or just swap m-th and n-th word? The dumbest way is
      to tokenize the whole sentence, split into words, place words in vector<string>, and then recomposed in desired order.
      Less dumb way is to leave [0..m] and [m, length] parts of string untouched.

      Comment

      Working...