常见的字符函数与字符串函数介绍(1)
常見(jiàn)的字符函數(shù)與字符串函數(shù)介紹
前言
C語(yǔ)言中對(duì)字符與字符串的處理很是頻繁,但是C語(yǔ)言中并沒(méi)有字符串類型的變量,字符串通常存放在常量字符串或者字符數(shù)組中。字符串常量適用于那些對(duì)它不做任何修改的字符串函數(shù)。
函數(shù)功能簡(jiǎn)介與再實(shí)現(xiàn)
1、 strlen函數(shù)
函數(shù)原型:size_t strlen (const char * str);
該函數(shù)用于計(jì)算字符串的長(zhǎng)度
函數(shù)簡(jiǎn)介
模擬實(shí)現(xiàn)
計(jì)數(shù)器方式實(shí)現(xiàn)函數(shù)
int my_strlen (const char * str) {int count = 0;while (*str){count++;str++;}return count; }不創(chuàng)建臨時(shí)變量計(jì)數(shù),使用遞歸方式
int my_strlen (const * str) {if(*str == '\0')return 0;elseretrun 1 + my_strlen(*str+1); }指針-指針的方式實(shí)現(xiàn)
int my_strlen (char * s) {char * p = s;while (*p != '\0')p++;return p-s; }三種方式均可以實(shí)現(xiàn)strlen函數(shù),都應(yīng)進(jìn)行掌握。
2、strcpy函數(shù)
函數(shù)原型 char * strcpy(char * destination, const char * source);
該函數(shù)用于實(shí)現(xiàn)字符串的拷貝
函數(shù)簡(jiǎn)介
destination, including the terminating null character (and stopping
at that point).
模擬實(shí)現(xiàn)
//1.參數(shù)順序 //2.函數(shù)功能與停止條件 //3.assert //4.const修飾指針 //5.函數(shù)的返回值 char* my_strcpy(char* dest, const char* src) {char* ret = dest;assert(dest != NULL);assert(src != NULL);while ((*dest++ = *src++)){;}return ret;3、strcat函數(shù)
函數(shù)原型`char * strcat(char * destination,const char * source);
該函數(shù)用于實(shí)現(xiàn)字符串的連接
函數(shù)簡(jiǎn)介
terminating null character indestination 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.
模擬實(shí)現(xiàn)
char* my_strcat(char* dest, const char* src) {char* ret = dest;while (*dest){dest++;}while (*dest++ = *src++){;}return ret; }4、strcmp函數(shù)
函數(shù)原型:int strcmp (const char * str1,const char * str2 );
該函數(shù)用于比較兩個(gè)字符串的大小
函數(shù)簡(jiǎn)介
If they are equal to each other, it continues with the following
pairs until the characters differ or until a terminating
null-character is reached.
第一個(gè)字符串等于第二個(gè)字符串,則返回0
第一個(gè)字符串小于第二個(gè)字符串,則返回小于0的數(shù)字
模擬實(shí)現(xiàn)
int my_strcmp(const char* src, const char* dst) {int ret = 0;assert(src != NULL);assert(dest != NULL);while (!(ret = *(unsigned char*)src - *(unsigned char*)dst) && *dst)++src, ++dst;if (ret < 0)ret = -1;else if (ret > 0)ret = 1;return(ret); }5、strncpy函數(shù)
函數(shù)原型:char * strncpy (char * destination,const char * source, size_t num);
該函數(shù)用于從源字符中拷貝num個(gè)字符到目標(biāo)空間。
函數(shù)簡(jiǎn)介·
模擬實(shí)現(xiàn)
#include<stdio.h> #include<stdlib.h>char* My_strncpy(const char*a, char* b, size_t sz) {size_t i = 0;for(i = 0; i < sz; i++)//拷貝n個(gè)字符{*(b+i) = *(a+i);}*(b+i) = '\0';return b; }int main() {char a[] = "asdfdgddh";char b[] = {0};char *p = My_strncpy(a, b, 3*sizeof(a[0]));printf("%s", p);system("pause");return 0; }6、strncat函數(shù)
函數(shù)原型:char * strncat(char * destination ,const char * source, size_t num);
該函數(shù)用于再在目標(biāo)空間的原有內(nèi)容之后追加num個(gè)字符
函數(shù)簡(jiǎn)介
strncat example
#include <stdio.h> #include <string.h> int main () {char str1[20];char str2[20];strcpy (str1,"To be ");strcpy (str2,"or not to be");strncat (str1, str2, 6);puts (str1);return 0; }運(yùn)行結(jié)果
To be or not模擬實(shí)現(xiàn)
#include<stdlib.h> #include<stdio.h> #include<assert.h>char *My_strncat(char *n_a, const char *n_b, size_t sz) {assert(n_a);assert(n_b);char *c = n_a;while(*n_a != '\0')//找到‘\0’的位置{n_a++;}while(sz--)//向后追加字符{*n_a++ = *n_b++;}*n_a = '\0';//最后結(jié)束時(shí)加'\0'return c; }int main() {char a[] = "asdfghjk";char b[] = "ZXCFGHJ";char* p = My_strncat(a, b, 3*sizeof(a[0]));printf("%s", p);system("pause");return 0; }7、strncmp函數(shù)
函數(shù)原型:int strncmp (const char * str1, sonst char *str2, size_t num);
該函數(shù)用于比較兩個(gè)字符串前num個(gè)字符的大小
函數(shù)簡(jiǎn)介
舉個(gè)栗子
#include <stdio.h> #include <string.h> int main () {char str[][5] = { "R2D2" , "C3PO" , "R2A6" };int n;puts ("Looking for R2 astromech droids...");for (n=0 ; n<3 ; n++)if (strncmp (str[n],"R2xx",2) == 0){printf ("found %s\n",str[n]);}return 0; }運(yùn)行結(jié)果
Looking for R2 astromech droids... found R2D2found R2A68、strstr函數(shù)
函數(shù)原型:char * strstr (const char * ,const char * );
該函數(shù)用于在arr1字符串中查找arr2字串第一次出現(xiàn)的位置,并返回其首地址
函數(shù)簡(jiǎn)介:
pointer if str2 is not part of str1.
舉個(gè)栗子
#include <stdio.h> #include <string.h> int main () {char str[] ="This is a simple string";char * pch;pch = strstr (str,"simple");strncpy (pch,"sample",6);puts (pch);return 0; }運(yùn)行結(jié)果
sample string模擬實(shí)現(xiàn)
char* my_strstr(const char* str1, const char* str2) {assert(str1);assert(str2);char* cp = (char*)str1;char* substr = (char*)str2;char* s1 = NULL;if (*str2 == '\0')return NULL;while (*cp){s1 = cp;substr = str2;while (*s1 && *substr && (*s1 == *substr)){s1++;substr++;}if (*substr == '\0')return cp;cp++;} }9、strtok函數(shù)
函數(shù)原型:char * strtok (char * str ,const char * sep);
函數(shù)簡(jiǎn)介
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è)字符串中被保存的位置開(kāi)始,查找下一個(gè)標(biāo)記。
如果字符串中不存在更多的標(biāo)記,則返回 NULL 指針。
具體解釋
char * arr[] = "lpt@bitedu.tech";其中,@與.稱為分隔符
char * p = "@,.";p為分隔符的合集
strtok(arr,p);此函數(shù)第一次調(diào)用,會(huì)將@改為\0,并返回l的地址
第二次調(diào)用,會(huì)將.改為\0,并返回b的地址
舉個(gè)栗子
#include <stdio.h> #include <string.h> int main () {char str[] ="- This, a sample string.";char * pch;printf ("Splitting string \"%s\" into tokens:\n",str);pch = strtok (str," ,.-");while (pch != NULL){printf ("%s\n",pch);pch = strtok (NULL, " ,.-");}return 0; }運(yùn)行結(jié)果
Splitting string "- This, a sample string." into tokens: This a sample string10、strerror函數(shù)
函數(shù)原型:char * strerror (int errum);
該函數(shù)為錯(cuò)誤報(bào)告函數(shù),會(huì)將錯(cuò)誤碼轉(zhuǎn)換為對(duì)應(yīng)的錯(cuò)誤信息
函數(shù)簡(jiǎn)介
| 0 | Not error |
| 1 | Operation not permitted |
| 2 | No much file or directory |
| 3 | No much process |
舉個(gè)栗子
#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)); //errno: Last error number return 0; } Edit & Run11、字符分類函數(shù)
| iscntrl | 任何控制字符 |
| isspace | 空白字符:空格‘ ’,換頁(yè)‘\f’,換行’\n’,回車‘\r’,制表符’\t’或者垂直制表符’\v’ |
| isdigit | 十進(jìn)制數(shù)字 0~9 |
| isxdigit | 十六進(jìn)制數(shù)字,包括所有十進(jìn)制數(shù)字,小寫字母a至f,大寫字母A~F |
| islower | 小寫字母a~z |
| isupper | 大寫字母A~Z |
| isalpha | 字母a至z或A~Z |
| isalnum | 字母或者數(shù)字,a~z, A~Z, 0~9 |
| ispunct | 標(biāo)點(diǎn)符號(hào),任何不屬于數(shù)字或者字母的圖形字符(可打印) |
| isgraph | 任何圖形字符 |
| isprint | 任何可打印字符,包括圖形字符和空白字符 |
12、字符轉(zhuǎn)換函數(shù)
int tolower (int c); int toupper (int c);舉例
#include <stdio.h> #include <string.h> int main() {int i = 0;char str[] = "Test String.\n";char c;while (str[i]){c = str[i];if (isupper(c))c = tolower(c);putchar(c);i++;}return 0; }運(yùn)行結(jié)果
test string.未完待續(xù)
總結(jié)
以上是生活随笔為你收集整理的常见的字符函数与字符串函数介绍(1)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 华为云客户端_华为公布云手机计费清单,要
- 下一篇: 旅游去