常见的字符函数与字符串函数介绍(1)
常見的字符函數與字符串函數介紹
前言
C語言中對字符與字符串的處理很是頻繁,但是C語言中并沒有字符串類型的變量,字符串通常存放在常量字符串或者字符數組中。字符串常量適用于那些對它不做任何修改的字符串函數。
函數功能簡介與再實現
1、 strlen函數
函數原型:size_t strlen (const char * str);
該函數用于計算字符串的長度
函數簡介
模擬實現
計數器方式實現函數
int my_strlen (const char * str) {int count = 0;while (*str){count++;str++;}return count; }不創建臨時變量計數,使用遞歸方式
int my_strlen (const * str) {if(*str == '\0')return 0;elseretrun 1 + my_strlen(*str+1); }指針-指針的方式實現
int my_strlen (char * s) {char * p = s;while (*p != '\0')p++;return p-s; }三種方式均可以實現strlen函數,都應進行掌握。
2、strcpy函數
函數原型 char * strcpy(char * destination, const char * source);
該函數用于實現字符串的拷貝
函數簡介
destination, including the terminating null character (and stopping
at that point).
模擬實現
//1.參數順序 //2.函數功能與停止條件 //3.assert //4.const修飾指針 //5.函數的返回值 char* my_strcpy(char* dest, const char* src) {char* ret = dest;assert(dest != NULL);assert(src != NULL);while ((*dest++ = *src++)){;}return ret;3、strcat函數
函數原型`char * strcat(char * destination,const char * source);
該函數用于實現字符串的連接
函數簡介
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.
模擬實現
char* my_strcat(char* dest, const char* src) {char* ret = dest;while (*dest){dest++;}while (*dest++ = *src++){;}return ret; }4、strcmp函數
函數原型:int strcmp (const char * str1,const char * str2 );
該函數用于比較兩個字符串的大小
函數簡介
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.
第一個字符串等于第二個字符串,則返回0
第一個字符串小于第二個字符串,則返回小于0的數字
模擬實現
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函數
函數原型:char * strncpy (char * destination,const char * source, size_t num);
該函數用于從源字符中拷貝num個字符到目標空間。
函數簡介·
模擬實現
#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個字符{*(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函數
函數原型:char * strncat(char * destination ,const char * source, size_t num);
該函數用于再在目標空間的原有內容之后追加num個字符
函數簡介
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; }運行結果
To be or not模擬實現
#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';//最后結束時加'\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函數
函數原型:int strncmp (const char * str1, sonst char *str2, size_t num);
該函數用于比較兩個字符串前num個字符的大小
函數簡介
舉個栗子
#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; }運行結果
Looking for R2 astromech droids... found R2D2found R2A68、strstr函數
函數原型:char * strstr (const char * ,const char * );
該函數用于在arr1字符串中查找arr2字串第一次出現的位置,并返回其首地址
函數簡介:
pointer if str2 is not part of str1.
舉個栗子
#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; }運行結果
sample string模擬實現
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函數
函數原型:char * strtok (char * str ,const char * sep);
函數簡介
sep參數是個字符串,定義了用作分隔符的字符集合
第一個參數指定一個字符串,它包含了0個或者多個由sep字符串中一個或者多個分隔符分割的標記。
strtok函數找到str中的下一個標記,并將其用 \0結尾,返回一個指向這個標記的指針。(注:strtok函數會改變被操作的字符串,所以在使用strtok函數切分的字符串一般都是臨時拷貝的內容并且可修改。)
strtok函數的第一個參數不為 NULL ,函數將找到str中第一個標記,strtok函數將保存它在字符串中的位置。
strtok函數的第一個參數為 NULL ,函數將在同一個字符串中被保存的位置開始,查找下一個標記。
如果字符串中不存在更多的標記,則返回 NULL 指針。
具體解釋
char * arr[] = "lpt@bitedu.tech";其中,@與.稱為分隔符
char * p = "@,.";p為分隔符的合集
strtok(arr,p);此函數第一次調用,會將@改為\0,并返回l的地址
第二次調用,會將.改為\0,并返回b的地址
舉個栗子
#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; }運行結果
Splitting string "- This, a sample string." into tokens: This a sample string10、strerror函數
函數原型:char * strerror (int errum);
該函數為錯誤報告函數,會將錯誤碼轉換為對應的錯誤信息
函數簡介
| 0 | Not error |
| 1 | Operation not permitted |
| 2 | No much file or directory |
| 3 | No much process |
舉個栗子
#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、字符分類函數
| iscntrl | 任何控制字符 |
| isspace | 空白字符:空格‘ ’,換頁‘\f’,換行’\n’,回車‘\r’,制表符’\t’或者垂直制表符’\v’ |
| isdigit | 十進制數字 0~9 |
| isxdigit | 十六進制數字,包括所有十進制數字,小寫字母a至f,大寫字母A~F |
| islower | 小寫字母a~z |
| isupper | 大寫字母A~Z |
| isalpha | 字母a至z或A~Z |
| isalnum | 字母或者數字,a~z, A~Z, 0~9 |
| ispunct | 標點符號,任何不屬于數字或者字母的圖形字符(可打印) |
| isgraph | 任何圖形字符 |
| isprint | 任何可打印字符,包括圖形字符和空白字符 |
12、字符轉換函數
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; }運行結果
test string.未完待續
總結
以上是生活随笔為你收集整理的常见的字符函数与字符串函数介绍(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为云客户端_华为公布云手机计费清单,要
- 下一篇: 旅游去