Ok, so I need a little bit of help with a program i'm writing. Let me give you the details.

The user is suppose to enter two sentences with all lower case letters and have the first letter of each sentence become capitalized.

Here is the code so far:

//  Assignment 5: Sentence Capitalizer
#include <iostream>
#include <cctype>
using namespace std;

// Function prototype
void capital(char*);



int main()
{
        char cstring[1024];

        // Get a string from the user.
        cout << "Enter at least two sentences, but do not use capital letters." << endl;
        cin.getline(cstring, 1024);

        // Capitalize the first word of each sentence.
        capital(cstring);

        // Display the resulting string.
        cout << "Here are your sentences with beginning words capitalized:" << endl;
        cout << cstring << endl << endl;
        return 0;
}

	void capital(char* cstring)
	{
		 
	}

Now I'm need to write the definition of the capital function so that it takes the two sentences I entered and capitalized the first letter of each sentence. How do I go about doing this? I know a little about toupper(), but how do you make focus on only certain letters? Is it through a while or for loop?
The hints I was given were these: The capital function is not given the length of the string. It should therefore iterate until the null
terminator character ('\0') is found

and

capital should capitalize the first letter following any punctuation character other than a comma.
This implies that the function will need to maintain a
flag (e.g. lastWasPunctuation) that indicates when the last non-space character that was processed was a
punctuation character.Note that the first non-space letter in the string should be capitalized.
This case can be handled by careful initialization of the lastWasPunctuation flag.

I appreciate any advice given. Thank you.

Recommended Answers

All 3 Replies

First make two variables, sentence1 and sentence2, unless you are
using a period '.' as the end of one sentence.

Use string if possible.

All you need to do is capitalize sentence[0], the first letter.
if its lower case then use the function from cctype, toupper(char).
It returns the uppercased version of the parameter passed.

first to operate the to upper function it looks a little like this

VariableName = toupper(variableName) although i do believe it can also be done like this (toupper(variableName))
Now i believe for your case you will need to have something that looks similar to this

The first line will be like this:

cstring[0] = toupper(cstring[0]); // Need to increment first letter of the first sentence

Problem is u will need to initialise a search looks for the first '.' character and then capitalises the character 2 places after that (assuming that there is a white space directly after the '.')

possibly have a while loop that looks like this

int x = 0;
while(cstring[x+2] != '\0') //stops the while loops after it has found a full stop or once it has reached the end of the array
{
        if(cstring[x] == '.')
        {
        cstring[x+2] = toupper(cstring[x+2]; //2 spots after the 'x' should be the first letter u need to capitalise
         }
    x++; // increment x so it checks the next spot
}

hope that helps a bit?

That should work, ill stare at it for anotehr 5 mins to see any little tweaks i cn make ^^

Both pieces of advice helped. Thanks guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.