Open carloscn opened 1 year ago
int32_t capitalize_title(char *title)
{
int32_t ret = 0;
size_t len = 0;
UTILS_CHECK_PTR(title);
UTILS_CHECK_LEN(len = strlen(title));
size_t word_len = 0;
for (size_t i = 0; i < len; i ++) {
if (title[i] != ' ') {
word_len ++;
} else {
break;
}
}
bool next_should_upper = (word_len > 2) ? true : false;
for (size_t i = 0; i < len; i ++) {
char e = title[i];
if (e == ' ') {
size_t j = i + 1;
word_len = 0;
while (title[j] != ' ' && title[j] != '\0') {
word_len ++, j ++;
}
next_should_upper = (word_len > 2) ? true : false;
continue;
}
if (islower(e) && next_should_upper == true) {
title[i] = utils_conv_uppercase(e);
}
if (isupper(e) && next_should_upper == false) {
title[i] = utils_conv_lowercase(e);
}
next_should_upper = false;
}
finish:
return ret;
}
Description
You are given a string title consisting of one or more words separated by a single space, where each word consists of English letters. Capitalize the string by changing the capitalization of each word such that:
If the length of the word is 1 or 2 letters, change all letters to lowercase. Otherwise, change the first letter to uppercase and the remaining letters to lowercase. Return the capitalized title.
Example 1:
Input: title = "capiTalIze tHe titLe" Output: "Capitalize The Title" Explanation: Since all the words have a length of at least 3, the first letter of each word is uppercase, and the remaining letters are lowercase.
Example 2:
Input: title = "First leTTeR of EACH Word" Output: "First Letter of Each Word" Explanation: The word "of" has length 2, so it is all lowercase. The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
Example 3:
Input: title = "i lOve leetcode" Output: "i Love Leetcode" Explanation: The word "i" has length 1, so it is lowercase. The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
Constraints:
1 <= title.length <= 100 title consists of words separated by a single space without any leading or trailing spaces. Each word consists of uppercase and lowercase English letters and is non-empty.