Given a word, you need to judge whether the usage of capitals in it is right or not.
We define the usage of capitals in a word to be right when one of the following cases holds:
- All letters in this word are capitals, like "USA".
- All letters in this word are not capitals, like "leetcode".
- Only the first letter in this word is capital if it has more than one letter, like "Google".
Example 1:
Input: "USA" Output: True
Example 2:
Input: "FlaG" Output: False
Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
其实确实不难,就是我的解法实在是太繁琐了,经过师姐提点才知道原来这样解判断首字母,大写的话,再判断第二个字母,大写的话后面出现小写字母就false
小写的话后面出现大写字母就false
小写的话,后面出现大写字母就false
感谢师父看了100行我之前恶习的代码,虽然我俩都觉得逻辑没问题,但就不知道错在哪里了,,,,,,
看了《志明与春娇》和《志明与春娇2》 现在上映的为什么不叫《志明与春娇3》
bool detectCapitalUse(char* word) {
int i=0;
int len=strlen(word);
if(('A'<=word[0])&&(word[0]<='Z')){
if(('A'<=word[1])&&(word[1]<='Z')){
for(i=2;i<len;i++){
if(('a'<=word[i])&&(word[i]<='z')){
return false;
}
}
}
else{
for(i=2;i<len;i++){
if(('A'<=word[i])&&(word[i]<='Z')){
return false;
}
}
}
return true;
}else{
for(i=1;i<len;i++){
if(('A'<=word[i])&&(word[i]<='Z')){
return false;
}
}
return true;
}
}