Hi, I am stuck and my head is spinning with this project that I am working on for my beginners c++ class. Can someone show me how to work this? Here is the problem:

Write a function that uses an array parameter to accept a string as its argument. It should convert the first letter of each word in the string to uppercase. If any of the letters are already uppercase, they should be left alone. (see hint). Demonstrate the function in a simple program that asks the user to input a string, passes it to a function, and then displays the string after it has been modified.

Here is the work I have so far: How do I just convert the first letter while keeping the rest lowercase or if uppercase leave it alone? also how do I make the numbers display as numbers? Thank you.

#include <iostream.h>

int main()
{

    char ch;


    cout << "Please enter a word: ";
    cin >> ch;

    if (ch >= 'A' && ch <= 'Z') 
        cout >> ch;
    else if (ch >= 'a' && ch <= 'z')
        cout >> toupper(char ch);
Dave Sinkula commented: Use code tags. +0

Walk along the string, but keep a boolean variable that's true if the last character was whitespace and false otherwise. Whenever you reach a non-whitespace character and the boolean variable is true, change the character to upper case. You don't need to worry about testing if it's already upper case because toupper does the right thing regardless of the character you give it.

I don't quite understand. Sorry , I'm pretty new at this... I worked on some more coding.. is this what you mean?


bool is_upper ( char ch )
{
static char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

for ( int i = 0; upper != '\0'; i++ ) {
if ( ch == upper )
return true;
}

return false;
}

bool is_lower ( char ch )
{
static char lower[] = "abcdefghijklmnopqrstuvwxyz";

for ( int i = 0; lower != '\0'; i++ ) {
if ( ch == lower )
return true;
}

return false;
}

can someone help please?

Greetings,

Thinking on the same line, lets walk along the string. Better yet, lets use a while loop to do so. By creating a seperate function to modify the string would probably be beneficial in this case. If we send the string to another function, we can treat it like a pointer allowing us to take the advantage of using the indirection or dereferencing operator *.

Pointers are not hard to understand, and I have written two tutorials on them. You can find them here:

Pointers (Part I): An Introduction
Pointers (Part II): Pointers In Application

Back on topic, we can create a function called checkString() if we wanted to. We will need to somehow retreive our string so lets make checkString() take one argument.

Let's look at what we can try:

#include <iostream>
#include <ctype.h>
using namespace std;

void checkString(char *str) {
	char lower[] = "abcdefghijklmnopqrstuvwxyz";

	while (*str) {
		// If not in the alphabet, increment and start again
		if (!isalpha(*str)) {
			str++;
			continue;
		}

		// If any lower letter was found in *str
		if (strchr(lower, *str)) {
			// Change to upper
			*str = toupper(*str);
		}

		// Increment location
		str++;
	}
}

Whoa! Lots to digest. Alright, lets start at the beginning. We take a single argument called str.
After this we create a local variable called lower. This contains all of our letters in the alphabet that are lower case.
Next we check if the current location of our pointer is a letter in the alphabet, though if not the loop continues and increments our array by one. If a letter is found using strchr() then we send it to upper case. The strchr() trick is quite neat. We know that strchr() checks for a letter in a string. Our letter is *str and our string is lower.
Next we increment the location of str. That is the only way to make the loop continue, as our while loop just checks if it exists and is not NULL.

Edit: Forgot to add that isalpha() is a ctype function. That is why we include ctype.h
All this does is tells us if the current location of our string is apart of the alphabet or not.

Just for an example, we could call on main() as the following:

int main (void) {
	char sentence[25] = "Hello There";

	checkString(sentence);

	cout << sentence << endl;

	return 0;
}

If you have further questions, please feel free to ask.


- Stack Overflow

>I don't quite understand.
What a surprise. Have you people even taken a problem solving course? A boolean variable is bool, it means true or false. Walk the string character by chraracter, you can do this with an int that counts from 0 to size - 1. Save whether the last character is a space. Then it's just a matter of an if statement. If this is too difficult for you then get your book out and start reading, because I really can't help you more than making the solution freaking obvious and as easy as possible.

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.