![]() |
|
#1
|
||||
|
||||
Need Code to Reverse words in a stringAlright heres my problem could anyone help me? I have no idea how to use strings This is NOT a HOMEWORK ASSIGNMENT!!
In this problem, we take the words of a sentence and rearrange the letters within each word. The first and last letters of each word are unchanged, and the letters in the rest of the word are reversed. For example, the words “School is Cool!” become “Soohcl is Cool!” under the transformation. The input for the problem consists of one sentence that has a several words where words are separated by space. The output should consist of transformed words. The order of the words should be preserved. The punctuation remains without change. For example, if the input was Today, I am writing lots of fun programs! the output should be: Tadoy, I am wnitirg ltos of fun pmargors! |
||||
|
#2
|
||||
|
||||
Re: Need Code to Reverse words in a stringSenior members might not reply to this thread if you:
We encourage you to post again once you've done that. __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#3
|
|||
|
|||
Re: Need Code to Reverse words in a stringUse pointer.
Malaysia Boleh. |
|
#4
|
|||
|
|||
Re: Need Code to Reverse words in a stringThis may give you some ideas:
CPP / C++ / C Code:
|
|
#5
|
||||
|
||||
Re: Need Code to Reverse words in a stringThis helps a little but the string has to be one that is input. How would i get that to work if im trying to randomize it like it said in the question in my first post
|
|
#6
|
|||
|
|||
Re: Need Code to Reverse words in a stringQuote:
CPP / C++ / C Code:
|
|
#7
|
|||
|
|||
Re: Need Code to Reverse words in a stringQuote:
Howard_L's point with fixing the input string is to focus on refining whatever algorithm is used to reverse characters within the string first. I agree with this point. It is always better to build upon a working solution as opposed to dealing with every possible complexity all at once. String input can be added later, & is not essential to any implementation of reversing characters. First, make the simple case work & then build upon it. At the same time, focusing on member functions accessible to cout is not particularly relevant at this point in discussion. Strings are ultimately a sequence & array of characters as in C. Nothing more. The core question lief480 is asking is how to reverse characters, and from the initial description, the first & last characters are never altered. So, the question becomes how to reverse internal characters. Obviously, strings of only two & three characters will remain untouched because in one case there are no internal characters or in the other case, only one character exists. Whatever algorithm is implemented must treat these special cases early on. So, this leaves two other cases:
I will leave it to you to convince yourself as to whether this method takes care of both an even & odd number of internal characters. QED. In deference to Howard_L's point (if I'm following where he was going...), member functions such as string::replace() might be used instead, but this might not be intuitively obvious. As an aside, this problem appears to be something that was in the newspapers about a year ago where psychologists had determined that randomized words could still be easily deciphered by most people as long as the first & last characters of the word were untouched. For what that may be worth. |
|
#8
|
|||
|
|||
Re: Need Code to Reverse words in a stringQuote:
re: above: ...as you can see I have trouble converting to all the fancy stuff of C++. C is stuck in my head! I was just trying to point out how to cycle through a string and how you can do things during each cycle. ie: count, copy, substitute, add... That's why I went the basic getc() putc() route. |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Trouble integrating console code into GUI | Barman007 | Java Forum | 18 | 15-May-2008 13:05 |
| Message Class | TransformedBG | C++ Forum | 5 | 29-Nov-2006 21:28 |
| a tester class and then some. | postage | Java Forum | 1 | 06-May-2006 15:48 |
| Help wit my source code compiler errors | Krandygrl00 | C++ Forum | 1 | 06-Jun-2005 08:14 |
| Reverse UTF encoded string | maaz | C++ Forum | 2 | 13-Apr-2005 12:33 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The