In Visual Basic 2008 Express edition: Create a new string replacing the order of the words, from last one to the first one and show it in a text box
Example:
Input: “This is an example”
Output: “example an is This”

I know theres a function to reverse the letters (StrReverse), but the order of the words? Please someone help me out.

Recommended Answers

All 3 Replies

I don't know if there are indexing routines, but you can set your starting position at 0. while it is less than the length of the string and the character at that position is blank go to the next starting position
set your ending length to 0 and while the length + position character are not blank and not past the end of the string you increment the length by 1. That identifies the length and position of the next word that you insert into a string array and increment the array by one.
Repeat that process using the new starting position until all the words are added to the array. Initalize a string with the last word in the array, from the next to last word to the beginning add a blank and that word to the string. This will not retain multiple blanks in the reversed string.

Try it,

Dim s As String = "This is an example"
s = String.Join(" ", s.Split(" ").ToArray().Reverse().ToArray())
commented: Perfect! +6

Thanks a lot, it worked perfectly. You just resumed the 30 lines i did in 2 lol. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.