介紹C語言中tolower函數(shù)的實例
C語言tolower函數(shù)用于把大寫字母轉(zhuǎn)換為小寫字母。
在本文中,我們先來介紹tolower函數(shù)的使用方法,然后編寫一個自定義的_tolower函數(shù),實現(xiàn)與tolower函數(shù)相同的功能。
1、包含頭文件
#include <ctype.h>
2、函數(shù)聲明
int tolower(int c);
3、功能說明
把大寫字母轉(zhuǎn)換為小寫字母,如果參數(shù)c不是大寫字母就不轉(zhuǎn)換,您可能會問:tolower函數(shù)的參數(shù)和返回值是整數(shù),不是字符,在C語言中,字符就是整數(shù),請補充學習一下基礎知識。
參數(shù)c為待轉(zhuǎn)換的字符。
返回值為轉(zhuǎn)換后的結(jié)果。
4、示例
#include <stdio.h>
int main()
{
printf("tolower('-')=%c\n",tolower('-'));
printf("tolower('0')=%c\n",tolower('0'));
printf("tolower('a')=%c\n",tolower('a'));
printf("tolower('A')=%c\n",tolower('A'));
}
運行效果

5、自定義的tolower函數(shù)的實現(xiàn)方法
在以下示例中,把自定義的tolower函數(shù)命名為_tolower。
程序的邏輯是:判斷參數(shù)c是否為大寫字母,如果是則加上32(小寫字母和大寫字母的ASCII碼值相差32),如果不是直接返回原字符。
#include <stdio.h>
// 自定義的tolower函數(shù)。
int _tolower(int c)
{
if (c>='A' && c<='Z') return c+32;
else return c;
}
int main()
{
printf("_tolower('-')=%c\n",_tolower('-'));
printf("_tolower('0')=%c\n",_tolower('0'));
printf("_tolower('a')=%c\n",_tolower('a'));
printf("_tolower('A')=%c\n",_tolower('A'));
}
運行效果

到此這篇關(guān)于介紹C語言中tolower函數(shù)的實例的文章就介紹到這了,更多相關(guān)C語言 tolower函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言全部內(nèi)存操作函數(shù)的實現(xiàn)詳細講解
這篇文章主要介紹了C語言全部內(nèi)存操作函數(shù)的實現(xiàn)詳細講解,作者用圖文代碼實例講解的很清晰,有感興趣的同學可以研究下2021-02-02
C語言讀取data.json文件并存入MySQL數(shù)據(jù)庫小案例(推薦)
本文介紹如何使用C語言結(jié)合cJSON庫讀取JSON文件,并將數(shù)據(jù)存儲到MySQL數(shù)據(jù)庫中,示例代碼包括創(chuàng)建MySQL表的SQL語句和C語言代碼,以及如何編譯和運行程序,確保已安裝必要的庫以支持程序運行2024-10-10

