日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

常见的字符函数与字符串函数介绍(1)

發布時間:2024/7/23 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 常见的字符函数与字符串函数介绍(1) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

常見的字符函數與字符串函數介紹

前言

C語言中對字符與字符串的處理很是頻繁,但是C語言中并沒有字符串類型的變量,字符串通常存放在常量字符串或者字符數組中。字符串常量適用于那些對它不做任何修改的字符串函數。

函數功能簡介與再實現

1、 strlen函數

函數原型:size_t strlen (const char * str);
該函數用于計算字符串的長度

函數簡介

  • 字符串一定以'\0'作為結束標志,strlen函數返回的是字符串'\0'之前出現的字符個數(不包括'\0')
  • 參數指向的字符串必須要以'\0'結束
  • 注意函數的返回值為size_t,是無符號的(易錯
  • 學會strlen函數的模擬實現
  • 模擬實現

    計數器方式實現函數

    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);
    該函數用于實現字符串的拷貝

    函數簡介

  • Copies the C string pointed by source into the array pointed by
    destination, including the terminating null character (and stopping
    at that point).
  • 源字符串必須以 ‘\0’ 結束。
  • 會將源字符串中的 ‘\0’ 拷貝到目標空間。
  • 目標空間必須足夠大,以確保能存放源字符串。
  • 目標空間必須可變。
  • 學會模擬實現
  • 模擬實現

    //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);
    該函數用于實現字符串的連接

    函數簡介

  • Appends a copy of the source string to the destination string. The
    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.
  • 源字符串必須以 '\0' 結束。
  • 目標空間必須有足夠的大,能容納下源字符串的內容。
  • 目標空間必須可修改。
  • 字符串自己不能給自己追加
  • 模擬實現

    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 );
    該函數用于比較兩個字符串的大小

    函數簡介

  • This function starts comparing the first character of each string.
    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
    第一個字符串小于第二個字符串,則返回小于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個字符到目標空間。

    函數簡介·

  • 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個字符從源字符串到目標空間
  • 如果源字符串的長度小于num,則拷貝完源字符串之后,在目標的后邊追加0,直到num個
  • 如果目標空間大小大于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個字符

    函數簡介

  • Appends the first num characters of source to destination, plus a terminating null-character.
  • If the length of the C string in source is less than num, only the content up to the terminating nullcharacter is copied.
  • 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個字符的大小

    函數簡介

  • 比較到出現另個字符不一樣或者一個字符串結束或者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 R2A6

    8、strstr函數

    函數原型:char * strstr (const char * ,const char * );
    該函數用于在arr1字符串中查找arr2字串第一次出現的位置,并返回其首地址

    函數簡介:

  • Returns a pointer to the first occurrence of str2 in str1, or a null
    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 string

    10、strerror函數

    函數原型:char * strerror (int errum);
    該函數為錯誤報告函數,會將錯誤碼轉換為對應的錯誤信息

    函數簡介

  • 必須包含的頭文件#include <errno.h>;
  • 錯誤碼錯誤信息
    0Not error
    1Operation not permitted
    2No much file or directory
    3No 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 & Run

    11、字符分類函數

    函數如果它的參數符合下列條件就返回為真
    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)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。