溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

c語言中如何將大寫字母轉(zhuǎn)成小寫

發(fā)布時間:2021-07-02 16:45:39 來源:億速云 閱讀:841 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)c語言中如何將大寫字母轉(zhuǎn)成小寫,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

#!/bin/bash
#name: upper_to_lower.sh
#the function is trun uper to lower
#like ABCd to abcd

haveuppernumber()
{
    #test if the string have upper number
    str="$(echo $1 | tr '[:upper:]' '[:lower:]')"
    if [ "$str" != $1 ] ; then #get some problem
        echo "[#have upper number,and i well trun them to lower:#]"
        return 1 #have upper number
    else
        return 0 #no upper number
    fi
}

if [ $# -ne 1 ] ; then
    echo "Usage: $0 <string>" >&2
    exit 1
fi

if ! haveuppernumber $1 ; then #when if is 0 it run?
#if [ 0 ] ; then #in shell true return 0 ,false return 1
    echo $1 | tr '[:upper:]' '[:lower:]' #it can turn the UPPER number to lower
#    echo $1 | tr '[:lower:]' '[:upper:]' #it can turn the lower number to UPPER
else
    echo "[#no upper number:#]"
    echo $1
fi

exit 0

功能說明:當(dāng)輸入”./upper_to_lower.sh AaBbCcdd“時會先判斷輸入格式是否正確,然后判斷字符串中是否有大寫字母如果有顯示"[#have upper number,and i well trun them to lower:#]"和轉(zhuǎn)換成小寫字母后的字符串;如果沒有大寫字母顯示"[#no upper number:#]"和小寫字符串。

c語言中如何將大寫字母轉(zhuǎn)成小寫

然后又試著用c語言實現(xiàn)相同的功能,如下:

代碼如下:


#include<stdio.h>
#include<stdlib.h>

int haveuppernumber(char *p)
{
    char*q=p;

    for(;*q!='\0';q++)
    {
        if(*q>='A'&&*q<='Z')
        {
            printf("[#have upper number and i will turn them to lower #]\n");
            return 1;
        }
    }
    printf("[#no upper number#]\n");
    return 0;
}
void turntolower(char *p)
{
    for(;*p != '\0';p++)
    {
        if(*p>='A' && *p<='Z')
        {
            *p+=' ';
        }
    }
}

int main(int argc , char *argv[])
{
    char *p;
    p=argv[1];

    if(argc != 2)
    {
        printf("Usage : %s <string>\n",argv[0]);
        exit(-1);
    }
    if(haveuppernumber(p))
    {
        turntolower(p);
        printf("%s\n",argv[1]);
    }
    else
    {
        printf("%s\n",argv[1]);
    }
    return 0;
}

c語言中如何將大寫字母轉(zhuǎn)成小寫

看完上述內(nèi)容,你們對c語言中如何將大寫字母轉(zhuǎn)成小寫有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI