creating a program that will capitalize first letters in proper nouns

looking for ideas on how to do this using cin.getline
I'll show some code, and then maybe you can try to figure it out from there.

See: http://www.cplusplus.com/reference/string/string/getline/

Have a list of nouns that you consider proper.
Use a while loop in combination with getline.
for each line the user enters, check if that word or phrase is in the list of proper nouns.
if it is, capitalize the first letter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cctype> // hint: std::tolower, std::toupper
#include <iostream>
#include <string>

int main()
{
    // Prompt user to enter words, each separated by a new line

    // provide an array that serves as a list of proper nounds
    // e.g.
    std::string proper_nouns[] = { "Apple", "Arabic", "Aramaic", "Charlie", "Gauss", "Greek", "Jesus", "Latin", "Lina Medina", "Paris", "Pope Urban II", "Tobago", "United States"};

    std::string output;

    std::string line;
    while (getline(std::cin, line))
    {
        // if (line matches a word inside proper_nouns) (Hint: compare all strings as lowercase)
        //      line = proper_nouns[index_of_match];

        output += line;
    }

    std::cout << output << std::endl;
}
Maybe like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <sstream>
#include <vector>


using namespace std;

vector<string> split(const string& line)
{
  vector<string> result;
  istringstream iss(line);
  string s;

  while(iss >> s)
    result.push_back(s);

  return result;
}

int main()
{
  string input;
  getline(cin, input);
  vector<string> words = split(input);
  for (string& word: words)
  {
    // if word is proper noun - see Ganado's post
    // then capitalize first letter
  }
  // print all the world
}
Is this really possible?

Consider "holly", "lily", "rose". Are they flowers (lower case) or girls' names (proper nouns)? In the case of "rose" it could even be the past tense of a verb: "Everyone stood when Rose rose from her chair".
Last edited on
Is this really possible?
Probably not, even spellchecker can't handle this.
I guess it's just a simple homework without special cases.
Real mature.
its intractable. we run into this at work a lot, where people go round and round about standardization and then it works on 90% of text but not some cases and they don't get it. This is why a lot of places all-caps your name when mailing etc.

The only way to do it is to have an AI that perfectly understands the text and can identify the context of the words and make a decision. And, we are not there yet (we are close but never perfect), see the bird and bicycle problem.

its probably best to keep politics out of the forum. Programmers already have a bad political reputation due to the antics of certain monopolistic giant corps, lets not make that worse or prove people right.

Last edited on
jonnin wrote:
its intractable.

It's tricky. Microsoft Word's grammar checker allows you to set grammar rules, but I have to be careful not to let it quietly make its own changes when there are people's names around. Even the rules about capitalisation (particularly with language) seem to be different in different countries ("He is English" -> "Il est anglais").

jonnin wrote:
its probably best to keep politics out of the forum.

My apologies, @jonnin and @ganado. I have removed it.
Last edited on
word lets you configure it so that it gets it right nearly 100% of the time for your personal use-cases. I was saying its intractable to make a general purpose solution.

speaking of word and politics... lol... A couple of bosses ago my boss knew a rather important politician (one of the contenders for POTUS ) and name dropped constantly. So I hacked his word so that every time he typed the guy's name it put in something rude, and it was driving him nuts. I think he finally gave up and reloaded his system to fix it.
Topic archived. No new replies allowed.