【知识补充及题解】《c语言入门》字符大小写

211 阅读1分钟

本文已参与[新人创作礼]活动,一起开启掘金创作之路

ASCALL码表



 大家可以看一看红色的区域就阔以了,其他的了解了解就可以。

大写转小写  +32 ,小写转大写  -32。

代码如下

 

 

 注意注意(思路来源于此)

 

*全部字母都是大写,比如 "USA" 。

*单词中所有字母都不是大写,比如 "leetcode" 。

*如果单词不只含有一个字母,只有首字母大写, 比如 "Google"。


 

力扣709.转换成小写字母  https://leetcode-cn.com/problems/to-lower-case/

 

char * toLowerCase(char * s){
int len=strlen(s);
for(int i=0;i<len;i++){
    if(s[i]>='A'&&s[i]<='Z'){
        s[i]=s[i]+32;   //大写转小写
    }
}return s;
}


力扣520.检测大写字母

leetcode-cn.com/problems/de…

 

bool detectCapitalUse(char * word){
int len=strlen(word);    //定义字符串的长度
int cnt=0;int m=0;         
for(int i=0;i<len;i++)   //遍历整个字符串
{
    if(word[i]>='A'&&word[i]<='Z') //大写字母区域
    cnt++;                         //找大写字母的个数
    else if(word[i]>='a'&&word[i]<='z') //小写字母区域
    m++;                           //m表示小写字母的个数
    
}
if(cnt==len||m==len)               //全为大写字母,或者全为小写字母
{
    return true;
}
else if(word[0]>='A'&&word[0]<='Z'&&m==len-1)return true;//只是首字母大写,其他均小写
else return false;
}