leetCode Detect Capital

1、问题描述

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:  

  1.All letters in this word are capitals, like "USA". All letters in this word are not capitals, like "leetcode".

       2.Only the first letter in this word is capital if it has more than one letter, like "Google".

       3.Otherwise, we define that this word doesn't use capitals in a right way.

 

    检测一个string中的大写字母的对错,以下三种情况中,大写字母的格式是正确的。

  1. 整个string全部是大写字母。

  2.整个string全是小写字母。

  3.单词中首字母大写。

 

2、问题分析

   首先处理特殊情况,输入的string 长度是0 或者 1的时候,返回 true。

  然后根据 前两个字母的大小分析。

 1 bool detectCapitalUse(string word)
 2     {
 3         if( word.size() == 0 || word.size() == 1 )
 4             return true;
 5         
 6         if( islower( word[0] ) )
 7         {
 8             for(int i = 1 ; i < word.size(); i++)
 9             {
10                 if( isupper(word[i]) )
11                     return false;
12             }
13         }
14         
15         if( isupper( word[0] )  && isupper(word[1]) )
16         {
17             for( int i = 2; i < word.size(); i++)
18                 if( islower( word[i]) )
19                     return false;
20         }
21         
22         if( isupper(word[0])  && islower(word[1]) )
23         {
24             for(int i = 2; i< word.size(); i++)
25                 if(isupper(word[i]))
26                     return false;
27         }      
28         
29         return true;
30     }

 

 1. 如果第一个字母是小写,那么后续字母中出现 大写字母,就返回false.

 2.如果前两个字母都是大写,那么后续的字母再出现小写字母就返回false。

  3.如果第一个字母是大写,第二个字母是小写,那么后续再出现大写字母,就返回false。

3、代码

 

转载于:https://www.cnblogs.com/wangxiaoyong/p/8652229.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值