Thread: Switching two words around in a string

  1. #1
    Registered User
    Join Date
    Jun 2008

    Switching two words around in a string

    I was wondering if someone could point me in the right direction here.

    A pointer to a string is passed into a function, the string contains a few different words and numbers separated by spaces. Anyways, the first two things in the string are two words that need to be switched around and then the string needs to be printed out again with the first two words switched around, everything else the same.

    I know this probably sounds simple to some, but I'm having a heck of a time figuring this out.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    Solve this problem by solving smaller problems first (remember, tiny strokes fell great oaks).

    First, build a function that can find and split up the two words given some string parameter.
    Then build another function that takes your string as input. Use the result of the first function and copy the two words over, switched. Return that result.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    When you're parsing a string for words, consider the string a sequence of letters and non-letters, where "letters" are what you want to consider as part of a word. (A good starting point might be a-z, A-Z, 0-9, and "_".)

    Once you have this defined, it's pretty easy to find the first word in a string. Skip any non-letters, and you have the start of the word. Pass over any letters, and you have the end of the word. Continue this process for as many words as you need.

    Now you have the words all marked out, the start and end indices of each word recorded. How can you swap a word? If you're creating a new array or just printing out the modified string, it's quite easy to do -- but it looks like you need to modify the same array. If the words are the same length it's also easy, but it's unlikely that they will all be the same length. However, note that the string will be the same length in the end (you're not adding or removing any characters, just shifting them around), so you don't have to resize the string.

    You might have to do some complicated juggling in the string to switch the words around. I'll let you might out the algorithm because I don't feel like it at the moment, but I should just mention one thing: memmove(), or a function like it, might be very useful.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Find first word. 
    Save the number which shows where the first lettter is.
    Save the length of the word. 
    For example first_letter1 = 5 (the 5th letter of the string), len1=10 (it has 10 letters)
    Find second word. Do as above. 
    Calculate also the length between the two words (since you read sequentially from a string it should be easy)
    You have all the information you need.
    Malloc an array as big as the length of the first word + lenght of second word + length of the space 
    in between the letters.
    Copy in that array first the second word (which would be my_string[first_letter2]). 
    Then copy everything in between. 
    Then copy the first word. 
    Then copy this array in the beggining of the first array.
    You will have what you want
    Last edited by C_ntua; 06-21-2008 at 04:34 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Converting a string of words into correct cases
    By BigFish21 in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2008, 12:53 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. New Theme
    By XSquared in forum A Brief History of
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM