?
?個(gè)人主頁:?熬夜學(xué)編程的小林
??系列專欄:?【C語言詳解】?【數(shù)據(jù)結(jié)構(gòu)詳解】
目錄
1、字符分類函數(shù)
2、字符轉(zhuǎn)換函數(shù)
3、strlen的使用和模擬實(shí)現(xiàn)
4、strcpy 的模擬實(shí)現(xiàn)
5、strcat 的模擬實(shí)現(xiàn)
6、strcmp 的模擬實(shí)現(xiàn)
7、strncpy 函數(shù)的使用
總結(jié)
1、字符分類函數(shù)

int islower ( int c );
#include <stdio.h>
#include <ctype.h>//判斷字符函數(shù)的頭文件
int main ()
{
int i = 0;
char str[] = "Test String.\n";
char c;
while (str[i])
{
c = str[i];
if (islower(c)) //如果是小寫字母則執(zhí)行
c -= 32;//大小寫之間ASCII碼值間隔32,也可以通過兩個(gè)字符相減,即'a'-'A';
putchar(c);//輸出該字符
i++;
}
return 0;
}
2、字符轉(zhuǎn)換函數(shù)
int tolower ( int c ); //將參數(shù)傳進(jìn)去的?寫字?轉(zhuǎn)?寫
int toupper ( int c ); //將參數(shù)傳進(jìn)去的?寫字?轉(zhuǎn)?寫
#include <stdio.h>
#include <ctype.h>
int main ()
{
int i = 0;
char str[] = "Test String.\n";
char c;
while (str[i])
{
c = str[i];//將該字符放在臨時(shí)變量中
if (islower(c)) //是小寫字母則轉(zhuǎn)換
c = toupper(c);
putchar(c);//輸出字符
i++;
}
return 0;
}
3、strlen的使用和模擬實(shí)現(xiàn)
注:在模擬實(shí)現(xiàn)函數(shù)時(shí),盡可能的將以下五個(gè)點(diǎn)都考慮到。
1.參數(shù)順序
2.const修飾指針(防止指針被修改)
3.函數(shù)的功能,停止條件4.assert(對(duì)空指針進(jìn)行判斷)
5.函數(shù)返回值文章來源:http://www.zghlxwxcb.cn/news/detail-826768.html
size_t strlen ( const char * str );
? 字符串以 '\0' 作為結(jié)束標(biāo)志,strlen函數(shù)返回的是在字符串中 '\0' 前面出現(xiàn)的字符個(gè)數(shù)(不包含 '\0' )。? 參數(shù)指向的字符串必須要以 '\0' 結(jié)束。? 注意函數(shù)的返回值為size_t,是無符號(hào)的( 易錯(cuò) )? strlen的使用需要包含頭文件? 學(xué)會(huì)strlen函數(shù)的模擬實(shí)現(xiàn)
#include <stdio.h>
#include <string.h>//strlen函數(shù)需包含的頭文件
int main()
{
const char* str1 = "abcdef";
const char* str2 = "bbb";
if(strlen(str2)-strlen(str1)>0)
{
printf("str2>str1\n");
}
else
{
printf("srt1>str2\n");
}
return 0;
}
首先參數(shù)順序,只有一個(gè)參數(shù)不需要管順序。第二個(gè)const修飾指針,此處只需遍歷數(shù)組不會(huì)修改字符,因此可以用const修飾指針。第三個(gè)函數(shù)功能是計(jì)算字符串長度,'\0'之前的長度。第四個(gè)assert斷言,傳參是指針,所以有可能是空指針,此處可以進(jìn)行斷言,空指針則報(bào)錯(cuò)。第五個(gè)返回值位無符號(hào)整數(shù),C語言提供無符號(hào)整數(shù)類型size_t,此處用int影響也不會(huì)很大,只是用size_t更加標(biāo)準(zhǔn)準(zhǔn)確。
//計(jì)數(shù)器方式
size_t my_strlen(const char * str)
{
int count = 0;
assert(str);//空指針則報(bào)錯(cuò)
while(*str)//遍歷字符串,遇到'\0'則結(jié)束循環(huán)
{
count++;
str++;
}
return count;//返回大小
}
//不能創(chuàng)建臨時(shí)變量計(jì)數(shù)器
size_t my_strlen(const char * str)
{
assert(str);//空指針則報(bào)錯(cuò)
if(*str == '\0')
return 0;
else
return 1+my_strlen(str+1);
}
//指針-指針的?式
size_t my_strlen(const char *s)
{
assert(str);
char *p = s;
while(*p != ‘\0’ )
p++;
return p-s;
}
4、strcpy 的模擬實(shí)現(xiàn)
char* strcpy(char * destination, const char * source );
? Copies the C string pointed by source into the array pointed by destination, including theterminating null character (and stopping at that point).將 source 指向的 C 字符串復(fù)制到目標(biāo)指向的數(shù)組中,包括終止 null 字符(并在該點(diǎn)停止)。? 源字符串必須以 '\0' 結(jié)束。? 會(huì)將源字符串中的 '\0' 拷貝到目標(biāo)空間。? 目標(biāo)空間 必須足夠大,以確保能存放源字符串。? 目標(biāo)空間必須可修改。? 學(xué)會(huì)模擬實(shí)現(xiàn)。
//1.參數(shù)順序 第一個(gè)為目標(biāo)字符串,第二個(gè)為原來字符串
//2.const修飾指針 目標(biāo)需要修改,原來不需要修改
//3.函數(shù)的功能,停?條件
//4.assert 兩個(gè)均不能為空指針
//5.函數(shù)返回值 返回目標(biāo)字符串首地址
//6.題?出?《?質(zhì)量C/C++編程》書籍最后的試題部分
char *my_strcpy(char *dest, const char*src)
{
char *ret = dest;
assert(dest != NULL);//為真則不報(bào)錯(cuò)
assert(src != NULL);//為真則不報(bào)錯(cuò)
while((*dest++ = *src++))
{
;
}
return ret;
}
5、strcat 的模擬實(shí)現(xiàn)
? Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.將源字符串的副本追加到目標(biāo)字符串。destination 中的終止 null 字符被 source 的第一個(gè)字符覆蓋,并且在 destination 中由兩者串聯(lián)形成的新字符串的末尾包含一個(gè) null 字符。? 源字符串必須以 '\0' 結(jié)束。? 目標(biāo)字符 串中也得有 ' \0'? ,否則沒辦法知道追加從哪里開始。? 目標(biāo) 空間必須有足夠的大,能容納下源字符串的內(nèi)容。? 目標(biāo) 空間必須可修改。? 字符串自己給自己追加,如何?
char *my_strcat(char *dest, const char*src)
{
char *ret = dest;
assert(dest != NULL);
assert(src != NULL);
while(*dest)
{
dest++;
}
while((*dest++ = *src++))
{
;
}
return ret;
}
6、strcmp 的模擬實(shí)現(xiàn)
? This function starts comparing the first character of each string. If they are equal to eachother, it continues with the following pairs until the characters differ or until a terminatingnull-character is reached.此函數(shù)開始比較每個(gè)字符串的第一個(gè)字符。如果它們相等,則繼續(xù)往下比較,直到字符不同或終止達(dá)到 null-character。? 標(biāo)準(zhǔn)規(guī)定:? 第?個(gè)字符串大于第?個(gè)字符串,則返回大于0的數(shù)字? 第?個(gè)字符串等于第?個(gè)字符串,則返回0? 第?個(gè)字符串小于第?個(gè)字符串,則返回小于0的數(shù)字? 那么如何判斷兩個(gè)字符串? 比較兩個(gè)字符串中對(duì)應(yīng)位置上字符ASCII碼值的大小。
int my_strcmp (const char * str1, const char * str2)
{
int ret = 0 ;
assert(src != NULL);
assert(dest != NULL);
while(*str1 == *str2)
{
if(*str1 == '\0')//兩個(gè)字符串相等且遇到'\0'即字符串相等,返回0
return 0;
str1++;
str2++;
}
return *str1-*str2;//不相等返回差值,>0即str1大,<0即str2大
}
7、strncpy 函數(shù)的使用
char * strncpy ( char * destination, const char * source, size_t num );
? Copies the first num characters of source to destination. If the end of the source C string(which is signaled by a null-character) is found before num characters have been copied,destination is padded with zeros until a total of num characters have been written to it.將源的前 num 個(gè)字符復(fù)制到目標(biāo)。如果源 C 字符串的末尾(由 null 字符表示)在復(fù)制 num 個(gè)字符之前找到,destination 用零填充,直到總共寫入了 num 個(gè)字符。? 拷貝num個(gè)字符從源字符串到目標(biāo)空間。? 如果源字符串的長度小于num,則拷貝完源字符串之后,在目標(biāo)的后邊追加0,直到num個(gè)。
#include <stdio.h>
#include <string.h>//strncpy函數(shù)需包含的頭文件
int main()
{
char str1[]="abcdeef";
char str2[20]={0};
strncpy(str2,str1,5);
printf("%s\n",str2);
return 0;
}

總結(jié)
本篇博客就結(jié)束啦,謝謝大家的觀看,如果公主少年們有好的建議可以留言喔,謝謝大家啦!文章來源地址http://www.zghlxwxcb.cn/news/detail-826768.html
到了這里,關(guān)于C語言第二十五彈---字符函數(shù)和字符串函數(shù)(上)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!