GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Oct-2008, 14:21
lief480's Avatar
lief480 lief480 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
lief480 is on a distinguished road

Need Code to Reverse words in a string


Alright 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  
Old 12-Oct-2008, 14:52
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,031
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Need Code to Reverse words in a string


Senior members might not reply to this thread if you:
  • used "Please help", "C problem" or something equally vague as the title for your thread.
  • included example code in your message and not use the syntax highlighting tags ([cpp], [java] etc.).
  • did not describe the problem accurately or include the error message (if any).
  • tried to get your homework assignment solved by someone else (and did not show your attempts at solving the problem)
The guidelines for posting can be found here.

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  
Old 12-Oct-2008, 19:36
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Need Code to Reverse words in a string


Use pointer.

Malaysia Boleh.
  #4  
Old 12-Oct-2008, 23:26
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 846
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Need Code to Reverse words in a string


This may give you some ideas:
CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main(void)
{
  char str[50] = "Today, I am writing lots of fun programs!";
  int i;

  cout << "c_string 'str' length is: " << strlen(str) << endl;

  for(i = 0; i < (int)sizeof(str); i++)
    cout.put(str[i]);     /* using array indicing is like using a pointer... 
                             you move from one data position to the next */  
  cout.put(0x0a);
  for(i = 0; i < (int)sizeof(str); i++)
  {
    if(str[i] == ' ')   
      str[i] = '_';
  
    if(str[i] == 'r') 
      str[i] = 'w';

    cout.put(str[i]);
  }
  cout.put(0x0a);

  return 0;
}
For more info on any of those items and tons more see: cplusplus.com/reference/iostream/
  #5  
Old 15-Oct-2008, 19:08
lief480's Avatar
lief480 lief480 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
lief480 is on a distinguished road

Re: Need Code to Reverse words in a string


This 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  
Old 15-Oct-2008, 20:44
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 846
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Need Code to Reverse words in a string


Quote:
but the string has to be one that is input... like it said in the question in my first post.
Learn to use the reference at the link I provided above. It shows a 'map' of the c++ llibraries. I know this is a tough concept, but you could search site for 'user input' or somesuch which upon doing you may find yourself heading in this direction: cplusplus.com/reference/iostream/istream/ ...where you might find a variety of functions to use. I like the humble cin.get();
CPP / C++ / C Code:
  /* note that this is the same loop as used before with a suprise inside */ 
  cout << "Now enter another string: " ;
  for(i = 0; i < (int)sizeof(str); i++)
  {
    cin.get( str[i] );   /* here we get() one character at a time */
    if(str[i] == ' ')
      str[i] = '_';
    if(str[i] == 'r')
      str[i] = 'w';
       cout.put(str[i]);
  }
  cout.put(0x0a);
There is even a tutorial at that site!
  #7  
Old 15-Oct-2008, 21:48
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 586
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Need Code to Reverse words in a string


Quote:
Originally Posted by lief480
This helps a little but the string has to be one that is input.
Both of you, lief480 & Howard_L, are sorta right, but at the same time, are sorta wrong.

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:
  • When the number of internal characters is even.
  • When the number of internal characters is odd.
Obviously, this means that lief480 will need to maintain two indexes or two pointers -- one at each end of the internal characters. Exchange the outermost characters, &:
  • Increment the left-most index/pointer.
  • Decrement the right-most index/pointer.
  • Exchange the characters at these new locations.
  • Repeat.
The stopping condition will be whenever both indexes/pointers point to the same character or the left-most index/pointer becomes larger than the right-most index/pointer.

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  
Old 16-Oct-2008, 08:51
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 846
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Need Code to Reverse words in a string


Quote:
as long as the first & last characters of the word were untouched
...that's interesting.
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 GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 03:39.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.