<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-17  評論-6  文章-1  trackbacks-0
    string.h中的函數(shù)

    @函數(shù)名稱:   strdup
    函數(shù)原型:   char *strdup(const char *s)
    函數(shù)功能:   字符串拷貝,目的空間由該函數(shù)分配
    函數(shù)返回:   指向拷貝后的字符串指針
    參數(shù)說明:   src-待拷貝的源字符串
    所屬文件:   <string.h>

    #include <stdio.h>
    #include <string.h>
    #include <alloc.h>
    int main()
    {
      char *dup_str, *string="abcde";
      dup_str=strdup(string);
      printf("%s", dup_str);
      free(dup_str);
      return 0;
    }


    @函數(shù)名稱:   strcpy
    函數(shù)原型:   char* strcpy(char* str1,char* str2);
    函數(shù)功能:   把str2指向的字符串拷貝到str1中去
    函數(shù)返回:   返回str1,即指向str1的指針
    參數(shù)說明:
    所屬文件:   <string.h>

    #include <stdio.h>
    #include <string.h>
    int main()
    {
      char string[10];
      char *str1="abcdefghi";
      strcpy(string,str1);
      printf("the string is:%s\n",string);
      return 0;
    }


    @函數(shù)名稱:   strncpy
    函數(shù)原型:   char *strncpy(char *dest, const char *src,int count)
    函數(shù)功能:   將字符串src中的count個字符拷貝到字符串dest中去
    函數(shù)返回:   指向dest的指針
    參數(shù)說明:   dest-目的字符串,src-源字符串,count-拷貝的字符個數(shù)
    所屬文件:   <string.h>

    #include <stdio.h>
    #include <string.h>
    int main()
    {
      char string[10];
      char *str1="abcdefghi";
      strncpy(string,str1,3);
      string[3]='\0';
      printf("%s",string);
      return 0;
    }


    @函數(shù)名稱:   strcat
    函數(shù)原型:   char* strcat(char * str1,char * str2);
    函數(shù)功能:   把字符串str2接到str1后面,str1最后的'\0'被取消
    函數(shù)返回:   str1
    參數(shù)說明:
    所屬文件:   <string.h>

    #include <stdio.h>
    #include <string.h>

    int main()
    {
      char buffer[80];

      strcpy(buffer,"Hello ");
      strcat(buffer,"world");
      printf("%s\n",buffer);
      return 0;
    }


    @函數(shù)名稱:   strncat
    函數(shù)原型:   char *strncat(char *dest, const char *src, size_t maxlen)
    函數(shù)功能:   將字符串src中前maxlen個字符連接到dest中
    函數(shù)返回:
    參數(shù)說明:
    所屬文件:   <string.h>

    #include <stdio.h>
    #include <string.h>

    char buffer[80];

    int main()
    {
      strcpy(buffer,"Hello ");
      strncat(buffer,"world",8);
      printf("%s\n",buffer);
      strncat(buffer,"*************",4);
      printf("%s\n",buffer);
      return 0;
    }


    @函數(shù)名稱:   strcmp
    函數(shù)原型:   int strcmp(char * str1,char * str2);
    函數(shù)功能:   比較兩個字符串str1,str2.
    函數(shù)返回:   str1<str2,返回負數(shù); str1=str2,返回 0; str1>str2,返回正數(shù).
    參數(shù)說明:
    所屬文件:   <string.h>

    #include <string.h>
    #include <stdio.h>
    int main()
    {
      char *buf1="aaa", *buf2="bbb", *buf3="ccc";
      int ptr;
      ptr=strcmp(buf2, buf1);
      if(ptr>0)
        printf("buffer 2 is greater than buffer 1\n");
      else
        printf("buffer 2 is less than buffer 1\n");
      ptr=strcmp(buf2, buf3);
      if(ptr>0)
        printf("buffer 2 is greater than buffer 3\n");
      else
        printf("buffer 2 is less than buffer 3\n");
      return 0;
    }


    @函數(shù)名稱:   strncmp
    函數(shù)原型:   int strncmp(char *str1,char *str2,int count)
    函數(shù)功能:   對str1和str2中的前count個字符按字典順序比較
    函數(shù)返回:   小于0:str1<str2,等于0:str1=str2,大于0:str1>str2
    參數(shù)說明:   str1,str2-待比較的字符串,count-比較的長度
    所屬文件:   <string.h>

    #include <string.h>
    #include <stdio.h>
    int main()
    {
      int ptr;
      char *buf1="aaabbb",*buf2="bbbccc",*buf3="ccc";
      ptr=strncmp(buf2,buf1,3);
      if (ptr>0)
        printf("buffer 2 is greater than buffer 1");
      else
        printf("buffer 2 is less than buffer 1");
        ptr=strncmp(buf2,buf3,3);
      if (ptr>0)
        printf("buffer 2 is greater than buffer 3");
      else
        printf("buffer 2 is less than buffer 3");
      return(0);
    }


    @函數(shù)名稱:   strpbrk
    函數(shù)原型:   char *strpbrk(const char *s1, const char *s2)
    函數(shù)功能:   得到s1中第一個“同時也出現(xiàn)在s2中”字符的位置指針
    函數(shù)返回:   位置指針
    參數(shù)說明:
    所屬文件:   <string.h>

    #include <stdio.h>
    #include <string.h>
    int main()
    {
    char *p="Find all vowels";

    while(p)
    {
      printf("%s\n",p);
      p=strpbrk(p+1,"aeiouAEIOU");
    }
    return 0;
    }


    @函數(shù)名稱:   strcspn
    函數(shù)原型:   int strcspn(const char *s1, const char *s2)
    函數(shù)功能:   統(tǒng)計s1中從頭開始直到第一個“來自s2中的字符”出現(xiàn)的長度
    函數(shù)返回:   長度
    參數(shù)說明:
    所屬文件:   <string.h>

    #include <stdio.h>
    #include <string.h>

    int main()
    {
      printf("%d\n",strcspn("abcbcadef","cba"));
      printf("%d\n",strcspn("xxxbcadef","cba"));
      printf("%d\n",strcspn("123456789","cba"));
      return 0;
    }


    @函數(shù)名稱:   strspn
    函數(shù)原型:   int strspn(const char *s1, const char *s2)
    函數(shù)功能:   統(tǒng)計s1中從頭開始直到第一個“不來自s2中的字符”出現(xiàn)的長度
    函數(shù)返回:   位置指針
    參數(shù)說明:
    所屬文件:   <string.h>

    #include <stdio.h>
    #include <string.h>
    #include <alloc.h>
    int main()
    {
      printf("%d\n",strspn("out to lunch","aeiou"));
      printf("%d\n",strspn("out to lunch","xyz"));
      return 0;
    }


    @函數(shù)名稱:   strchr
    函數(shù)原型:   char* strchr(char* str,char ch);
    函數(shù)功能:   找出str指向的字符串中第一次出現(xiàn)字符ch的位置
    函數(shù)返回:   返回指向該位置的指針,如找不到,則返回空指針
    參數(shù)說明:   str-待搜索的字符串,ch-查找的字符
    所屬文件:   <string.h>

    #include <string.h>
    #include <stdio.h>
    int main()
    {
      char string[15];
      char *ptr, c='r';
      strcpy(string, "This is a string");
      ptr=strchr(string, c);
      if (ptr)
        printf("The character %c is at position: %d\n",c,ptr-string);
      else
        printf("The character was not found\n");
      return 0;
    }


    @函數(shù)名稱:   strrchr
    函數(shù)原型:   char *strrchr(const char *s, int c)
    函數(shù)功能:   得到字符串s中最后一個含有c字符的位置指針
    函數(shù)返回:   位置指針
    參數(shù)說明:
    所屬文件:   <string.h>

    #include <string.h>
    #include <stdio.h>
    int main()
    {
      char string[15];
      char *ptr,c='r';
      strcpy(string,"This is a string");
      ptr=strrchr(string,c);
      if (ptr)
        printf("The character %c is at position:%d",c,ptr-string);
      else
        printf("The character was not found");
      return 0;
    }


    @函數(shù)名稱:   strstr
    函數(shù)原型:   char* strstr(char* str1,char* str2);
    函數(shù)功能:   找出str2字符串在str1字符串中第一次出現(xiàn)的位置(不包括str2的串結(jié)束符)
    函數(shù)返回:   返回該位置的指針,如找不到,返回空指針
    參數(shù)說明:
    所屬文件:   <string.h>

    #include <stdio.h>
    #include <string.h>
    int main()
    {
      char *str1="Open Watcom C/C++",*str2="Watcom",*ptr;
      ptr=strstr(str1,str2);
      printf("The substring is:%s\n",ptr);
      return 0;
    }


    @函數(shù)名稱:   strrev
    函數(shù)原型:   char *strrev(char *s)
    函數(shù)功能:   將字符串中的所有字符顛倒次序排列
    函數(shù)返回:   指向s的指針
    參數(shù)說明:
    所屬文件:   <string.h>

    #include <string.h>
    #include <stdio.h>
    int main()
    {
      char *forward="string";
      printf("Before strrev():%s",forward);
      strrev(forward);
      printf("After strrev(): %s",forward);
      return 0;
    }


    @函數(shù)名稱:   strnset
    函數(shù)原型:   char *strnset(char *s, int ch, size_t n)
    函數(shù)功能:   將字符串s中前n個字符設(shè)置為ch的值
    函數(shù)返回:   指向s的指針
    參數(shù)說明:
    所屬文件:   <string.h>

    #include <stdio.h>
    #include <string.h>
    int main()
    {
      char *string="abcdefghijklmnopqrstuvwxyz";
      char letter='x';
      printf("string before strnset: %s",string);
      strnset(string,letter,13);
      printf("string after strnset: %s",string);
      return 0;
    }


    @函數(shù)名稱:   strset
    函數(shù)原型:   char *strset(char *s, int ch)
    函數(shù)功能:   將字符串s中所有字符設(shè)置為ch的值
    函數(shù)返回:   指向s的指針
    參數(shù)說明:
    所屬文件:   <string.h>

    #include <stdio.h>
    #include <string.h>
    int main()
    {
      char string[10]="123456789";
      char symbol='c';
      printf("Before strset(): %s", string);
      strset(string, symbol);
      printf("After strset(): %s", string);
      return 0;
    }


    @函數(shù)名稱:   strtok
    函數(shù)原型:   char *strtok(char *s1, const char *s2)
    函數(shù)功能:   分解s1字符串為用特定分隔符分隔的多個字符串(一般用于將英文句分解為單詞)
    函數(shù)返回:   字符串s1中首次出現(xiàn)s2中的字符前的子字符串指針
    參數(shù)說明:   s2一般設(shè)置為s1中的分隔字符
            規(guī)定進行子調(diào)用時(即分割s1的第二、三及后續(xù)子串)第一參數(shù)必須是NULL
            在每一次匹配成功后,將s1中分割出的子串位置替換為NULL(摘下鏈中第一個環(huán)),因此s1被破壞了
            函數(shù)會記憶指針位置以供下一次調(diào)用
           
    所屬文件:   <string.h>

    #include <string.h>
    #include <stdio.h>
    int main()
    {
      char *p;
      char *buffer;
      char *delims={ " .," };

      buffer=strdup("Find words, all of them.");
      printf("%s\n",buffer);
      p=strtok(buffer,delims);
      while(p!=NULL){
        printf("word: %s\n",p);
        p=strtok(NULL,delims);
      }
      printf("%s\n",buffer);
      return 0;
    }


    @函數(shù)名稱:   strupr
    函數(shù)原型:   char *strupr(char *s)
    函數(shù)功能:   將字符串s中的字符變?yōu)榇髮?BR>函數(shù)返回:
    參數(shù)說明:
    所屬文件:   <string.h>

    #include <stdio.h>
    #include <string.h>
    int main()
    {
      char *string="abcdefghijklmnopqrstuvwxyz",*ptr;
      ptr=strupr(string);
      printf("%s",ptr);
      return 0;
    }


    @函數(shù)名稱:   strlwr
    函數(shù)原型:   char *strlwr(char *s)
    函數(shù)功能:   將字符串中的字符變?yōu)樾懽址?BR>函數(shù)返回:   指向s的指針
    參數(shù)說明:
    所屬文件:   <string.h>

    #include<string.h>
    int main()
    {
      char str[]="HOW TO SAY?";
      printf("%s",strlwr(str));
      return 0;
    }


    @函數(shù)名稱:   strlen
    函數(shù)原型:   unsigned int strlen(char * str);
    函數(shù)功能:   統(tǒng)計字符串str中字符的個數(shù)(不包括終止符'\0')
    函數(shù)返回:   返回字符串的長度.
    參數(shù)說明:
    所屬文件:   <string.h>

    #include <stdio.h>
    #include<string.h>
    int main()
    {
      char str[]="how are you!";
      printf("the lence is:%d\n",strlen(str));
      return 0;
    }


    @函數(shù)名稱:   strerror
    函數(shù)原型:   char *strerror(int errnum)
    函數(shù)功能:   得到錯誤信息的內(nèi)容信息
    函數(shù)返回:   錯誤提示信息字符串指針
    參數(shù)說明:   errnum-錯誤編號
    所屬文件:   <string.h>

    #include <stdio.h>
    #include <errno.h>
    int main()
    {
      char *buffer;
      buffer=strerror(errno);
      printf("Error: %s",buffer);
      return 0;
    }


    @函數(shù)名稱:   memcpy
    函數(shù)原型:   void *memcpy(void *dest, const void *src, size_t n)
    函數(shù)功能:   字符串拷貝
    函數(shù)返回:   指向dest的指針
    參數(shù)說明:   src-源字符串,n-拷貝的最大長度
    所屬文件:   <string.h>,<mem.h>

    #include <stdio.h>
    #include <string.h>
    int main()
    {
      char src[]="******************************";
      char dest[]="abcdefghijlkmnopqrstuvwxyz0123456709";
      char *ptr;
      printf("destination before memcpy:%s\n",dest);
      ptr=memcpy(dest,src,strlen(src));
      if (ptr)
        printf("destination after memcpy:%s\n",dest);
      else
        printf("memcpy failed");
      return 0;
    }


    @函數(shù)名稱:   memccpy
    函數(shù)原型:   void *memccpy(void *dest, const void *src, int c, size_t n)
    函數(shù)功能:   字符串拷貝,到指定長度或遇到指定字符時停止拷貝
    函數(shù)返回:
    參數(shù)說明:   src-源字符串指針,c-中止拷貝檢查字符,n-長度,dest-拷貝底目的字符串指針
    所屬文件:   <string.h>,<mem.h>

    #include <string.h>
    #include <stdio.h>
    int main()
    {
      char *src="This is the source string";
      char dest[50];
      char *ptr;
      ptr=memccpy(dest,src,'c',strlen(src));
      if (ptr)
      {
        *ptr='\0';
        printf("The character was found:%s",dest);
      }
      else
        printf("The character wasn't found");
      return 0;
    }


    @函數(shù)名稱:   memchr
    函數(shù)原型:   void *memchr(const void *s, int c, size_t n)
    函數(shù)功能:   在字符串中第開始n個字符中尋找某個字符c的位置
    函數(shù)返回:   返回c的位置指針,返回NULL時表示未找到
    參數(shù)說明:   s-要搜索的字符串,c-要尋找的字符,n-指定長度
    所屬文件:   <string.h>,<mem.h>

    #include <string.h>
    #include <stdio.h>
    int main()
    {
      char str[17];
      char *ptr;
      strcpy(str,"This is a string");
      ptr=memchr(str,'r',strlen(str));
      if (ptr)
      printf("The character 'r' is at position: %d",ptr-str);
      else
      printf("The character was not found");
      return 0;
    }


    @函數(shù)名稱:   memcmp
    函數(shù)原型:   int memcmp(const void *s1, const void *s2,size_t n)
    函數(shù)功能:   按字典順序比較兩個串s1和s2的前n個字節(jié)
    函數(shù)返回:   <0,=0,>0分別表示s1<,=,>s2
    參數(shù)說明:   s1,s2-要比較的字符串,n-比較的長度
    所屬文件:   <string.h>,<mem.h>

    #include <stdio.h>
    #include <string.h>
    int main()
    {
      char *buf1="ABCDE123";
      char *buf2="abcde456";
      int stat;
      stat=memcmp(buf1,buf2,5);
      printf("The strings to position 5 are ");
      if(stat) printf("not ");
      printf("the same\n");
      return 0;
    }



    @函數(shù)名稱:   memicmp
    函數(shù)原型:   int memicmp(const void *s1, const void *s2, size_t n)
    函數(shù)功能:   按字典順序、不考慮字母大小寫對字符串s1,s2前n個字符比較
    函數(shù)返回:   <0,=0,>0分別表示s1<,=,>s2
    參數(shù)說明:   s1,s2-要比較的字符串,n-比較的長度
    所屬文件:   <string.h>,<mem.h>

    #include <stdio.h>
    #include <string.h>
    int main()
    {
      char *buf1="ABCDE123";
      char *buf2="abcde456";
      int stat;
      stat=memicmp(buf1,buf2,5);
      printf("The strings to position 5 are ");
      if(stat) printf("not");
      printf("the same");
      return 0;
    }


    @函數(shù)名稱:   memmove
    函數(shù)原型:   void *memmove(void *dest, const void *src, size_t n)
    函數(shù)功能:   字符串拷貝
    函數(shù)返回:   指向dest的指針
    參數(shù)說明:   src-源字符串,n-拷貝的最大長度
    所屬文件:   <string.h>,<mem.h>

    #include <string.h>
    #include <stdio.h>
    int main()
    {
      char dest[40]="abcdefghijklmnopqrstuvwxyz0123456789";
      printf("destination prior to memmove:%s\n",dest);
      memmove(dest+1,dest,35);
      printf("destination after memmove:%s",dest);
      return 0;
    }


    @函數(shù)名稱:   memset
    函數(shù)原型:   void *memset(void *s, int c, size_t n)
    函數(shù)功能:   字符串中的n個字節(jié)內(nèi)容設(shè)置為c
    函數(shù)返回:
    參數(shù)說明:   s-要設(shè)置的字符串,c-設(shè)置的內(nèi)容,n-長度
    所屬文件:   <string.h>,<mem.h>

    #include <string.h>
    #include <stdio.h>
    #include <mem.h>
    int main()
    {
      char buffer[]="Hello world";
      printf("Buffer before memset:%s\n",buffer);
      memset(buffer,'*',strlen(buffer)-1);
      printf("Buffer after memset:%s",buffer);
      return 0;
    }

    posted on 2006-03-09 16:54 小鐵匠 閱讀(2007) 評論(0)  編輯  收藏 所屬分類: c/c++
    主站蜘蛛池模板: 亚洲一级特黄大片无码毛片 | 成年女人午夜毛片免费看 | 国产免费不卡v片在线观看| 亚洲日韩欧洲无码av夜夜摸| 91av免费在线视频| 久久亚洲AV永久无码精品| v片免费在线观看| 中文字幕亚洲一区| 免费福利在线视频| 亚洲成电影在线观看青青| 日韩在线免费视频| 亚洲精品乱码久久久久久蜜桃图片| jizz免费在线影视观看网站| 国产精品亚洲mnbav网站| 中文字幕无码毛片免费看| 欧洲亚洲国产清在高| 18禁黄网站禁片免费观看不卡| 国产在线19禁免费观看| 免费无码AV一区二区| 亚洲成人国产精品| 特级做A爰片毛片免费看无码| 国产婷婷高清在线观看免费| 国产一区二区三区亚洲综合 | 色婷婷六月亚洲综合香蕉| 亚洲精品无码日韩国产不卡?V| 亚洲人成黄网在线观看| 成人爽A毛片免费看| 免费无毒a网站在线观看| 久久久久无码精品亚洲日韩 | 久久久久亚洲国产AV麻豆| 亚洲人成无码www久久久| 国产无遮挡色视频免费观看性色| 免费看美女被靠到爽| 一级午夜免费视频| 亚洲日产2021三区在线| 免费观看理论片毛片| 国产午夜精品免费一区二区三区| 亚洲国产香蕉人人爽成AV片久久| 亚洲天堂男人影院| 久久精品国产亚洲沈樵| 免费看香港一级毛片|