Separating a string into words

  • crimsonclear
  • Born
  • Born
  • crimsonclear
  • Posts: 2

Post 3+ Months Ago

alright, lets just say that i think my comp sci 1 teacher bought his phd over the internet, cause i didnt learn too much. This is our first assignment for Comp Sci 2 and i'm pretty lost.

heres the problem:

Write a C++ program that will process the user’s full name (i.e., first, middle, and last name –> Joe Bob Allen is an example) and display the name in various ways. There is only one word allowed for the first name and one word allowed for the last name, but the middle name can consist of multiple words. The maximum length of the full name is 75. The maximum length of the first name is 25. The maximum length of the last name is 25.

For the little i do know, i would think it would be easiest just to create a loop that breaks off words at the spaces in the string. We dont actually have to output the middle names, so i'm guessing there would be another loop that starts at the end of the string and works its way up (to get the last name).

Any help would be much appreciated
  • Lieven
  • Beginner
  • Beginner
  • User avatar
  • Posts: 57
  • Loc: Belgium

Post 3+ Months Ago

I don't know which string class you have to use, but if you can use the cstring class, the only methods you need are find, left and right...

CString name( "John Foo Bar Doe" );
int spaceposition = name.Find( ' ' );

with name.Left(), you can grab the firstname.

after that, you can loop with name.Find until you find the last space and use name.Right to grab the last name.

http://msdn.microsoft.com/library/en-us/vcmfc98/html/_mfc_cstring.3a3a.find.asp?frame=true

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 71 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.