MANIPULATE A C-STRINGS AS AN ARRAY OF CHARACTERS

Hi guys please help me on this to problem

1.Write a program using the cin.get() and toupper() library functions, along with a cout stream object to display each entered letter in its uppercase form. The program should terminate when the 1 key (the digit) is pressed.

2a. Write a C++ program that stops reading a line of text when a period is entered and displays the sentence with correct spacing and capitalization. For this program, correct spacing means only one space should be used between words, and all letters should be lowercase, except the first letter. For example, if the user enters the text I am going to Go TO The moVies., the displayed sentence should be I am going to go to the movies.

2b. Determine what characters, if any, aren’t displayed correctly by the program you created for Question 2a.
What help do you need? Nobody here is going to do your homework for you. We'll answer questions and help you when you are stuck, but posting a homework problem and expecting correct code doesn't fly.
since you should be learning strings, Ill give you some key tips for char arrays.

toupper(letter) gives the upper case.
if you don't need to "stop reading" so you can resume reading later, you can just do this:
cin.getline(str,size); //get the whole line.
char* cp = strstr(str,"."); //find the period, if any.
if(cp) //if a period, set it to magic end of string char, ending the string in the right place.
cp[0] = 0;

now to write it out, the easy way is to iterate until you hit the zero char,
.. write the first letter in caps.
.. write all the rest in lower case
.. if you encoutner 2+ spaces in a row, only write one of them.
.. write out period if required at the end (unclear).

all the above can be done in a single fairly simple loop. It may be easier to write the first character first, then loop over the rest, though.

2b presumably you did it correctly.







Topic archived. No new replies allowed.