There's a title format called "title case" where the first letter of each word is capitalized, and the rest of the letters are lowercase. However, certain articles, prepositions, and conjunctions like "the, a, an, in, on, at, of, for, by, to, and, or, but" should only be capitalized if they are the first or last word in the title.
For example, Ernest Hemingway's work "For Whom the Bell Tolls" appears in the aforementioned format as "For Whomthe Bell Tolls", and F. Scott Fitzgerald's work "The Great Gatsby" appears in English title as "The Greal Gaisby". Please write a program that, given a title, converts it to title case according to the rules of "title case".
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The input is a string of characters with a length not exceeding 500 characters, containing only uppercase and lowercase English letters. Words are separated by a single space. | Please output a string containing the corrected title according to the "title case" rule. |
ironweed | Ironweed |
WiDe sargaSSO | Wide Sargasso |
The SOUND and FURY | The Sound and Fury |
for whom the bell tolls | For Whom the Bell Tolls |
The catcher In THe rye | The Catcher in the Rye |
what men live by | What Men Live By |
Thought Process
Use EOF to collect strings, and each time data is received, push that string back into a vector.
Convert all letters to lowercase first. If the string is the first or last one, capitalize the first character. If it's neither and is a special word (the, a, an, in, on, at, of, for, by, to, and, or, but), don't change it and output as it is. If none of these conditions are met, capitalize the first character and output.
Sample Code-ZeroJudge K515: Title
#include <iostream>
#include <vector>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
string str;
vector<string>v;
while (cin >> str) v.push_back(str);
for (int i = 0; i<v.size(); i++) {
for (int j = 0; j<v[i].length(); j++) v[i][j] = tolower(v[i][j]);
if (i == 0 || i == v.size()-1) {
v[i][0] = toupper(v[i][0]);
cout << v[i] << " ";
continue;
}
string s = v[i];
if (s == "the" || s == "a" || s == "an" || s == "in" || s == "on" || s == "at" || s == "of" || s == "for" || s == "by" || s == "to" || s == "and" || s == "or" || s == "but") {
cout << s << " ";
}
else {
s[0] = toupper(s[0]);
cout << s << " ";
}
}
cout << "\n";
}
//ZeroJudge K515
//Dr. SeanXD