You are here:

C++/You passed judgement too early


Mr. Mcardell, I previously wrote you about a small program. I did not want you to do the program for me I meerly needed some guidance.  But your other customers you helped are pleased I guess thats all that counts in your line of work. I assume when I am able to finish my programming class' I would be as arrogant as you sir.  Have a good day.

Sorry you feel that way. However my response was to your question, which read:

Subject: C++  Question

Create a string containing  full name & outputs each part the name separetly. Name should be First, Mid, & last name separated by each other by a single space.

This is all I know of you. I am not clairvoyant and at no point in the question text do you ask for help with any aspect of this problem, or say what you are having problems with. It is not a question it is a command: "Create a string...". This immediately lead me to the conclusion you had just posted some homework or assignment, and were too lazy or arrogant yourself to even bother re-phrasing the problem as a question and just wanted a solution to pass off as your own. I get quite a few of these (some have even offered money for a solution!) and your question fitted the form.

If you wish for help getting started on such problems or with any other aspect of them then please, please, please give me some sort of a hint as to what it is you wish me to help with.

Something along the lines of:

"I have been set an assignment. I do not know where to start and would like some guidance please. The assignment is as follows:

Create a string containing  full name & outputs each part the name separetly. Name should be First, Mid, & last name separated by each other by a single space."

would be more helpful to me. It starts out stating what you wish me to help with and shows me the problem you are trying to solve.

Additional information that can be useful are which platform and compiler you are using (e.g. Windows XP and DevC++) and maybe what sort of things you have been covering in your class, so I can gauge the answer to what you (hopefully) know already.

The more context I have about the question (and questioner to some extent) the more useful to you my answer is likely to be.

Anyhow, now you have indicated that all you want is some guidance I can help, although not knowing what C++ you have been taught so far my answer may not be 100% relevant.

So, the first part is to create a string. This is not difficult, especially if we assume the use of the standard C++ library, which contains a string class. In this case we would include the standard C++ header string at the start of the program source file, outside of any functions:

   #include <string>

Then in the function we wish to create the string in we define a variable of the type std::string. The std part of the name indicates that the string type is in the std namespace, which is reserved for names of things in the C++ standard library:

   std::string theString;

Of course many C++ classes teach a more C style of programming, which has no string type. Whether this is the case here I do not know, but I'll cover it anyway.

In C strings are handled by arrays of characters with a zero character appended to the end, and these are available in C++ as well. As these are built in types no header file would need to be included to create such a string:

   const int MaxNumberOfCharactersInString( 100 );
   char theString[MaxNumberOfCharactersInString+1];

Notice that we have to give the number of characters in the character array for the C-style string. This is given as 1 more than the maximum number of characters we wish to have in the string, defined as 100 by the constant MaxNumberOfCharactersInString. The extra character space in the character array for the string is for the terminating zero character.

OK, so that is how to create strings. However these are either empty (for std::string) or uninitialised (for char array). The problem states we are to have a full name in the string of the form "First Mid Last" with each part separated by 1 space. We can do that like so:

   std::string theString("First Mid Last");
   std::string theString = "First Mid Last";

And for the character array case:

   char theString[] = "First Mid Last";

Note that we did not have to specify the size of the character array string. This is because the compiler works it out for us from the length of the string literal used to initialise it.

So that's the easy part, now for the crux of the problem. I am going to assume the point here is for you to split the string into its parts by hand as it were and not use provided functions for operating on strings to help. Maybe I am wrong - again it comes down to what information you give me in the question. Before going into that you should have a main program entry point function that looks something like:

   int main()
   // code

Whether you place all the program logic into the main function or split it into functions is up to you. In general the latter is a better approach, as it gives programs a better structure. However this program is not very long so maybe it is OK to place it all into main - we shall see how thing develop as we go.

So you have a string containing the full name of someone (theString in my examples), with each component separated by exactly a single space.

The problem is to output each part of the name separately. Note that nothing is said about storing the parts separately, only that they should be output separately. This may allow us some simplification of the solution. My thought here would be to create a function that outputs one part of the name each time it is called, or nothing at all if it has no more parts to output. The reason for creating a separate function is to reinforce the separateness of the parts being output, as this is a specific requirement of the problem. We can then write something like:

   std::cout << "First name: ";
   WriteOnePart( theString, std::cout );

And repeat for mid name and last name. The WriteOnePart function would write characters to the passed std::ostream until it reached a space or the end of the string whereupon it would return. This can be achieved using a loop that iterates through the string outputting characters until a space is reached when it stops and returns. In pseudo code it might look something like:

   While current character not string end
     AND current character not space
       Output current character to output stream
       Increment current character position
   End While

If you are following carefully you might ask how we track where to start in the string as the starting character positions for mid and last name parts are not at the start of the string - they will be somewhere in the middle of the string, so how does WriteOnePart know these positions?

A good question. There are several ways to handle this problem. Probably the simplest is to pass the start position into the function as another argument and have WriteOnePart return the position of the next part, or an end-of-string position value. This would make our main code look something like:

   PositionType partPosition( <string start position value> );
   std::cout << "First name: ";
   partPosition = WriteOnePart( theString, partPosition, std::cout );

   if ( partPosition == <end of string position value> )
   // take unexpected end of string action

   std::cout << "\nMiddle name: ";
   partPosition = WriteOnePart( theString, partPosition, std::cout );

Now I have left the specifics of the type used for positions and the values for start and end positions vague as it can depend on what sort of string type you are using.

However, maybe the simplest approach would be to use an integer type as a position index and use array element access syntax (e.g. theString[position]) to access the characters at these positions:

   int partPosition( 0 ); // C and C++ index arrays from zero

This has the advantage that it works with both std::string and char array strings.

Detecting the end of the string for character array strings can be done by checking if the character at the current position is the zero character value ('\0'). In the case of std::string you can compare the position value to the length of the string (theString.length() or theString.size()). If the position is equal (or greater!) than the string length then you have reached the end. In this case make sure you do not try to access the character at this position as you will be past the end of the string.

The pseudo code for the revised WriteOnePart would look like:

   While current character not string end
     AND current character not space
       Output current character to output stream
       Increment current character position
   End While

   Return current character position

The way I have tackled the problem is only one way to achieve a solution. If you need further guidance or think the solution too advanced please ask a follow up question - remembering to give specifics of what you need help with!

Oh, and experts for AllExperts are volunteers so you and other questioners are not really customers of mine.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.