user2881555
user2881555

Reputation: 141

How to return a word with the first letter capitalised

I have the following:

string ProperNoun::GetWord() {
    word[0] = toupper(word[0]);
    return (word);
}

I am having trouble trying to make it so the word that is returned from the word getter makes the first letter capitalised, if it is a propernoun, using the above code it makes the word unfindable in my dictionary, I assumed it makes the word capitalized and then searches hence I changed the word I'm searching to have a capital letter though it still wasn't finding it. My question is how can I make my code capitalise the first letter if its a propernoun.

Upvotes: 0

Views: 200

Answers (1)

Chris Olsen
Chris Olsen

Reputation: 3511

You could do the capitalization in a second word getter to be used for display only. This getter would not modify the stored word.

string ProperNoun::GetWordForDisplay() {
    string s = word;
    s[0] = toupper(s[0]);
    return s;
}

Alternatively, you could add a second word getter for use in dictionary searches. This getter could return a lowercase copy of the word.

string ProperNoun::GetWordForSearch() {
    string s = word;
    std::transform(s.begin(), s.end(), s.begin(), ::tolower);
    return s;
}

Upvotes: 1

Related Questions