?個(gè)人主頁(yè):?熬夜學(xué)編程的小林
??系列專欄:?【C語(yǔ)言詳解】?【數(shù)據(jù)結(jié)構(gòu)詳解】
目錄
1、strncat 函數(shù)的使用
2、strncmp 函數(shù)的使用
3、strstr 函數(shù)的使用和模擬實(shí)現(xiàn)
4、strtok 函數(shù)的使用
5、strerror 函數(shù)的使用
6、perror 函數(shù)的使用
總結(jié)文章來源:http://www.zghlxwxcb.cn/news/detail-830022.html
1、strncat 函數(shù)的使用
char * strncat ( char * destination, const char * source, size_t num );
? Appends the first num characters of source to destination, plus a terminating null-character.(將source指向字符串的前num個(gè)字符追加到destination指向的字符串末尾,再追加?個(gè) \0 字符)。? If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.(如果source 指向的字符串的長(zhǎng)度小于num的時(shí)候,只會(huì)將字符串中到 \0 的內(nèi)容追加到destination指向的字符串末尾)。
/* strncat example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[20];
char str2[20];
strcpy (str1,"To be ");//將"To be "拷貝到str1
strcpy (str2,"or not to be");//將"or not to be"拷貝到str2
strncat (str1, str2, 6);//將str2前面6個(gè)字符接到str1末尾
printf("%s\n", str1);//打印str1
return 0;
}
2、strncmp 函數(shù)的使用
int strncmp ( const char * str1, const char * str2, size_t num );
比較str1和str2的前num個(gè)字符,如果相等就繼續(xù)往后比較,最多比較num個(gè)字母,如果提前發(fā)現(xiàn)不?樣,就提前結(jié)束,大的字符所在的字符串大于另外?個(gè)。如果num個(gè)字符都相等,就是相等返回0.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20];
char str2[20];
strcpy(str1, "abcdef");//將"abcdef"拷貝到str1
strcpy(str2, "abc");//將"abc"拷貝到str2
int ret=strncmp(str1, str2, 4);//將返回值給賦值給ret
if (ret > 0)
printf("str1 > str2\n");
else if (ret < 0)
printf("str1 < str2\n");
else
printf("str1 == str2\n");
return 0;
}

3、strstr 函數(shù)的使用和模擬實(shí)現(xiàn)
char * strstr ( const char * str1, const char * str2);
Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.(函數(shù)返回字符串str2在字符串str1中第?次出現(xiàn)的位置)。The matching process does not include the terminating null-characters, but it stops there.(字符串的比較匹配不包含 \0 字符,以 \0 作為結(jié)束標(biāo)志)。
/* strstr example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="This is a simple string";
char * pch;
pch = strstr (str,"simple");//找到與simple相等的首地址賦值給pch
strncpy (pch,"sample",6);//將sample前6個(gè)字符拷貝到pch 即修改str中的simple
printf("%s\n", str);//打印str
return 0;
}

char * strstr (const char * str1, const char * str2)
{
char *cp = (char *) str1;//str1為const修飾指針,需強(qiáng)制轉(zhuǎn)化為可改指針
char *s1, *s2;
if ( !*str2 )//str2為空返回str1首地址
return((char *)str1);//返回值為char*,需強(qiáng)轉(zhuǎn)成char*
while (*cp)
{
s1 = cp;
s2 = (char *) str2;
while ( *s1 && *s2 && !(*s1-*s2) )//*s1 s2不為'\0'且*s1==*s2,
s1++, s2++;
if (!*s2)//*s2為'\0'則返回cp
return cp;
cp++;//否則cp++
}
return NULL;//沒有找到則返回NULL
}
4、strtok 函數(shù)的使用
char * strtok ( char * str, const char * sep);
? sep參數(shù)指向?個(gè)字符串,定義了用作分隔符的字符集合? 第?個(gè)參數(shù)指定?個(gè)字符串,它包含了0個(gè)或者多個(gè)由sep字符串中?個(gè)或者多個(gè)分隔符分割的標(biāo)記。? strtok函數(shù)找到str中的下?個(gè)標(biāo)記,并將其用? \0 結(jié)尾,返回?個(gè)指向這個(gè)標(biāo)記的指針。(注: strtok函數(shù)會(huì)改變被操作的字符串,所以在使用strtok函數(shù)切分的字符串?般都是臨時(shí)拷貝的內(nèi)容并且可修改。)? strtok函數(shù)的第?個(gè)參數(shù)不為?NULL ,函數(shù)將找到str中第?個(gè)標(biāo)記,strtok函數(shù)將保存它在字符串中的位置。? strtok函數(shù)的第?個(gè)參數(shù)為 NULL ,函數(shù)將在同?個(gè)字符串中被保存的位置開始,查找下?個(gè)標(biāo)記。? 如果字符串中不存在更多的標(biāo)記,則返回 NULL 指針。
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "192.168.6.111";
char* sep = ".";
char* str = NULL;
for (str = strtok(arr, sep); str != NULL; str = strtok(NULL, sep))
{
printf("%s\n", str);
}
return 0;
}
5、strerror 函數(shù)的使用
char * strerror ( int errnum );
strerror函數(shù)可以把參數(shù)部分錯(cuò)誤碼對(duì)應(yīng)的錯(cuò)誤信息的字符串地址返回來。在不同的系統(tǒng)和C語(yǔ)?標(biāo)準(zhǔn)庫(kù)的實(shí)現(xiàn)中都規(guī)定了?些錯(cuò)誤碼,?般是放在 errno.h 這個(gè)頭文件中說明的,C語(yǔ)言程序啟動(dòng)的時(shí)候就會(huì)使用?個(gè)全面的變量errno來記錄程序的當(dāng)前錯(cuò)誤碼,只不過程序啟動(dòng)的時(shí)候errno是0,表示沒有錯(cuò)誤,當(dāng)我們?cè)谑褂脴?biāo)準(zhǔn)庫(kù)中的函數(shù)的時(shí)候發(fā)?了某種錯(cuò)誤,就會(huì)講對(duì)應(yīng)的錯(cuò)誤碼,存放在errno中,而?個(gè)錯(cuò)誤碼的數(shù)字是整數(shù)很難理解是什么意思,所以每?個(gè)錯(cuò)誤碼都是有對(duì)應(yīng)的錯(cuò)誤信息的。strerror函數(shù)就可以將錯(cuò)誤對(duì)應(yīng)的錯(cuò)誤信息字符串的地址返回。
#include <errno.h>
#include <string.h>
#include <stdio.h>
//我們打印?下0~10這些錯(cuò)誤碼對(duì)應(yīng)的信息
int main()
{
int i = 0;
for (i = 0; i <= 10; i++) {
printf("%s\n", strerror(i));
}
return 0;
}
No error? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 沒有錯(cuò)誤
Operation not permitted? ? ? ? ?不允許操作
No such file or directory? ? ? ? ?沒有這樣的文件或目錄
No such process? ? ? ? ? ? ? ? ? ? 沒有這樣的過程
Interrupted function call? ? ? ? ?中斷的函數(shù)調(diào)用
Input/output error? ? ? ? ? ? ? ? ? ?輸入/輸出誤差
No such device or address? ? 沒有這樣的設(shè)備或地址
Arg list too long? ? ? ? ? ? ? ? ? ? ? 參數(shù)列表太長(zhǎng)
Exec format errorExec? ? ? ? ? ?格式錯(cuò)誤
Bad file descriptor? ? ? ? ? ? ? ? ? 錯(cuò)誤的文件描述符
No child processes? ? ? ? ? ? ? ? 無子進(jìn)程
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main ()
{
FILE * pFile;
pFile = fopen ("unexist.ent","r");
if (pFile == NULL)
printf ("Error opening file unexist.ent: %s\n", strerror(errno));
return 0;
}
Error opening file unexist.ent: No such file or directory
?
6、perror 函數(shù)的使用
void perror ( const char * str );
將 errno 的值解釋為錯(cuò)誤消息,并將其打印到 stderr(標(biāo)準(zhǔn)錯(cuò)誤輸出流,通常是控制臺(tái)),可以選擇在它前面加上 str 中指定的自定義消息。
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main ()
{
FILE * pFile;
pFile = fopen ("unexist.ent","r");
if (pFile == NULL)
perror("Error opening file unexist.ent");
return 0;
}
Error opening file unexist.ent: No such file or directory
總結(jié)
本篇博客就結(jié)束啦,謝謝大家的觀看,如果公主少年們有好的建議可以留言喔,謝謝大家啦!文章來源地址http://www.zghlxwxcb.cn/news/detail-830022.html
到了這里,關(guān)于C語(yǔ)言第二十六彈---字符串函數(shù)(下)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!