You are here:

C++/Character manipulation question in C++


I am a college student taking a introduction class to C++ language. I am not going to ask you to do the homework for me. Reason being, I rather learn it and get whatever grade I deserve than turning in a homework that I have no idea of.

I just need to know -Where should I start and -What is the key(hint) to this program?

Program requires:
"Write a program to convert a series of words entered in by the user. This is a character manipulation exercise, do not use string data types or arrays of characters!"
-Every word should be capitalized
-Remove all punctuation
-If any character are uppercase, display them as lowercase
-Remove any repeating characters in a row(ex. silly would be sily)

I found out cin.get(ch) is the way to read each character one by one.
Also learned that using "toupper" function is the way to capitalize first letter of the every word.

However, I can't seem to "organize my mind" with all the things I've learned/read so far. How would this program look like? Would it involve various of user-defined functions?

I honestly think I have habit of rather thinking way TOO hard, stressing myself out and end up giving up.

If you can just kind of give me some "hints" on how to tackle this program, I would really really appreciate it.

Thank you so much for taking your time reading this.


OK the first thing to say is:

        Don't panic! (in large friendly letters...)

The second thing is to not get it all working at once. Do it one step at a time.

OK you have found how to read in a character. Can you write it out again?

When you can do this you can put the two parts together:

Read a character
Write the character read in

This is all very well but you also have to do it for more than one character, so wrap the above steps in a loop:

While not finished
       Read in a character
       Write out the read in character
End loop

Or maybe:

       Read in a character
       Write out the read in character
While not finished

Which leads to the question: When are we finished? Your description of the problem (a specification) did not mention the termination condition. Check the full specification of the problem you have been given and if the termination condition is still not clear check with your client (your professor or lecturer or teacher).

We are supposed to process the data input before output. A classic program form: Input, Process, Output or IPO.

So now our outline looks like so:

       Read in a character
       Process read in character
       Write out the processed character
While not finished

Next we look at the specification and see we are asked to read in a series of words. So what defines a word? Usually for this sort of use words can be defined as a sequence of characters separated by white space - a term usually used to describe space, tab and new line characters. Maybe if a newline ('\n') is entered it can also be used as the termination condition, so we only read one line from the user. If this is a reasonable assumption then not finished translates as:

       The reading in character is not a new line character.

So we have defined words as character sequences surrounded by white space and defined what white space is. What do we need to do with words?

- make the first character upper case
- make the other characters lower case
- remove any punctuation characters
- remove any repeated characters in the word

It does _not_ say what to do if the word contains numeric digit 0, 1, 2, 3 etc. It also does not say in which order the rules should be applied. For example if I enter:


Should I get:


output because the first character of the 'word' is punctuation and making it upper case does nothing, and then it is removed.

Or should we remove punctuation and only apply the make first character of word uppercase rule when we have a letter (or digit presumably!), which would give the more reasonable output:


First let us detect words:

If the read in character is a white space character then we have finished a word.

The _next_ (valid) character read in will need to be made uppercase.

We can control the state using Boolean flags. In this case if we hit white space, and finish a word we have to reset to some state. Obvious names would be startOfWord or wordNotStarted. The latter is a more accurate description of the state. It also should be set to start with (i.e. when the input process output loop is entered). Once set it remains set until a non-whitespace character is read, when it is reset to false. This look likes so:

Bool wordNotStarted( true );

       Read in a character

       Processed character becomes equal to read in character
       If read in character is whitespace:
         wordNotStarted = true
       End If
         If wordNotStarted:
         Make Processed character uppercase
         wordNotStarted = false
         End If
       End Else     

       Write out the Processed character
While not finished

Of course this does not do any of the other processing. There are various ways to handle the various cases of action based on input and state. The above way is one way: lots of if else if else if ... tests and actions. Another way would be to use a switch statement:

switch ( readInChar )
case ' ':
case '\t':
case '\n': // Whitespace enterred
  // actions

case '\'':
case '"':
case ',':
case '.':  // punctuation entered
// etc...
   // actions

  // actions to take for all characters not mentioned in a case

The reason I mention this is that as you incrementally add support for the other requirements - stripping punctuation, making non-first word characters lowercase, removing repeated characters the number of decisions will increase and may become messy. This may be one way around it. Although from experience with the C character and string functions there are some that would be useful that preclude the use of the switch statement approach - ispunct, isalpha, isalnum, isspace etc.. in <cctype> (C++ library header equivalent to C <ctype.h>).

So get as far as I have described in outline. Then add support for the other processing, again one step at a time. The last requirement, no repeats, is odd given the injunction against character arrays, string class types, and presumably std::vector<char> as it implies you have to store all current processed characters for each word so as to see if you have one yet. You might like to look at std::set (#include <set>), maybe it is allowable.

Good luck.


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.