# Thread: Switching two words around in a string

1. ## 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. 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. 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.

4. Pseudo-code
Code:
```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```

Popular pages Recent additions