0

The program is supposed to use a function that accepts a pointer to a C-string as an argument and capitalizes the first character of each sentence in the string. I'm having trouble with the output. This is my code:

#include "stdafx.h"
#include <cstring>
#include <iostream>

using namespace std;

void Capitalize(char *);

int _tmain(int argc, _TCHAR* argv[])
{
    char sentence[1000];


    cout << "Please enter a sentence: " << endl;
    cin.getline(sentence, 1000);

    char *sentencePtr = sentence;


    Capitalize(sentencePtr);

    cout << sentencePtr;

    cin.get();
    return 0;
}

void Capitalize(char *str){
    int count;

    for(count = 0; count < strlen(str); count++){

        if(str[count] = '.'){

            count += 2;

            toupper(str[count]);

        }



    }

}
1
  • What trouble are you having? Can you please give some example input and output? Commented Dec 15, 2011 at 7:29

5 Answers 5

2
        toupper(str[count]);

This converts the character to upper case and then throws the result away. You want:

        str[count]=toupper(str[count]);

Also, this is an assignment:

    if(str[count] = '.'){

You want a comparison:

    if(str[count] == '.'){
Sign up to request clarification or add additional context in comments.

Comments

0

That's a good go, but toupper returns the uppercase version of a character, it does not modify the argument that is provided. Try this:

 // note, you must use '==', not '='
 if(str[count] == '.')
 {
        count += 2;
        str[count] = toupper(str[count]);
 }

As an exercise, try and avoid using C-strings altogether, and see if you can do it using only the std::string class. Ultimately, you will realise that using std::string is much easier than using plain old C-strings.

Comments

0

You're using an assignment operator (=) not a comparison (==), you need to change:

if(str[count] = '.'){

To:

if(str[count] == '.'){

As others have indicated, your use of toupper isn't quite right either, as it returns the new value, it doesn't modify the orignal as it doesn't take a reference.

str[count] = toupper(str[count]);

Comments

0

You have a typo here:

if(str[count] = '.')

should be:

if(str[count] == '.')

Also, str[count] = toupper(str[count]);

Comments

0

Looks like your comparison is wrong. Try changing

if(str[count] = '.')

to

if(str[count] == '.'){

remember -> = is a assignment operator == is a comparison operator

(I think your Capitalize func is wrong, but i dont know if its how you want it to be)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.