zoukankan      html  css  js  c++  java
  • C语言字符串函数大全

    C语言字符串函数大全

    函数名: stpcpy

    功 能: 拷贝一个字符串到另一个

    用 法: char *stpcpy(char *destin, char *source);

    程序例:

     1 #include <stdio.h>
     2 
     3 #include <string.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char string[10];
    12 
    13     char *str1 = "abcdefghi";
    14 
    15  
    16 
    17     stpcpy(string, str1);
    18 
    19     printf("%s
    ", string);
    20 
    21     return 0;
    22 
    23 }

    函数名: strcat

    功 能: 字符串拼接函数

    用 法: char *strcat(char *destin, char *source);

    程序例:

     1 #include <string.h>
     2 
     3 #include <stdio.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char destination[25];
    12 
    13     char *blank = " ", *c = "C++", *Borland = "Borland";
    14 
    15  
    16 
    17     strcpy(destination, Borland);
    18 
    19     strcat(destination, blank);
    20 
    21     strcat(destination, c);
    22 
    23  
    24 
    25     printf("%s
    ", destination);
    26 
    27     return 0;
    28 
    29 }

    函数名: strchr

    功 能: 在一个串中查找给定字符的第一个匹配之处

    用 法: char *strchr(char *str, char c);

    程序例:

     1 #include <string.h>
     2 
     3 #include <stdio.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char string[15];
    12 
    13     char *ptr, c = ‘r‘;
    14 
    15  
    16 
    17     strcpy(string, "This is a string");
    18 
    19     ptr = strchr(string, c);
    20 
    21     if (ptr)
    22 
    23     printf("The character %c is at position: %d
    ", c, ptr-string);
    24 
    25     else
    26 
    27     printf("The character was not found
    ");
    28 
    29     return 0;
    30 
    31 }

    函数名: strcmp

    功 能: 串比较

    用 法: int strcmp(char *str1, char *str2);

    看Asic码,str1>str2,返回值 > 0;两串相等,返回0

    程序例:

     1 #include <string.h>
     2 
     3 #include <stdio.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
    12 
    13     int ptr;
    14 
    15  
    16 
    17     ptr = strcmp(buf2, buf1);
    18 
    19     if (ptr > 0)
    20 
    21     printf("buffer 2 is greater than buffer 1
    ");
    22 
    23     else
    24 
    25     printf("buffer 2 is less than buffer 1
    ");
    26 
    27  
    28 
    29     ptr = strcmp(buf2, buf3);
    30 
    31     if (ptr > 0)
    32 
    33     printf("buffer 2 is greater than buffer 3
    ");
    34 
    35     else
    36 
    37     printf("buffer 2 is less than buffer 3
    ");
    38 
    39  
    40 
    41     return 0;
    42 
    43 }
    44 
    45  

    函数名: strncmpi

     能: 将一个串中的一部分与另一个串比较, 不管大小写

     法: int strncmpi(char *str1, char *str2, unsigned maxlen);

    程序例:

     

     1 #include <string.h>
     2 
     3 #include <stdio.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char *buf1 = "BBB", *buf2 = "bbb";
    12 
    13     int ptr;
    14 
    15  
    16 
    17     ptr = strcmpi(buf2, buf1);
    18 
    19  
    20 
    21     if (ptr > 0)
    22 
    23     printf("buffer 2 is greater than buffer 1
    ");
    24 
    25  
    26 
    27     if (ptr < 0)
    28 
    29     printf("buffer 2 is less than buffer 1
    ");
    30 
    31  
    32 
    33     if (ptr == 0)
    34 
    35     printf("buffer 2 equals buffer 1
    ");
    36 
    37  
    38 
    39     return 0;
    40 
    41 }

     

    函数名: strcpy

     能: 串拷贝

     法: char *strcpy(char *str1, char *str2);

    程序例:

     

     1 #include <stdio.h>
     2 
     3 #include <string.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char string[10];
    12 
    13     char *str1 = "abcdefghi";
    14 
    15  
    16 
    17     strcpy(string, str1);
    18 
    19     printf("%s
    ", string);
    20 
    21     return 0;
    22 
    23 }

     

    函数名: strcspn

     能: 在串中查找第一个给定字符集内容的段

     法: int strcspn(char *str1, char *str2);

    程序例:

     

     1 #include <stdio.h>
     2 
     3 #include <string.h>
     4 
     5 #include <alloc.h>
     6 
     7  
     8 
     9 int main(void)
    10 
    11 {
    12 
    13     char *string1 = "1234567890";
    14 
    15     char *string2 = "747DC8";
    16 
    17     int length;
    18 
    19  
    20 
    21     length = strcspn(string1, string2);
    22 
    23     printf("Character where strings intersect is at position %d
    ", length);
    24 
    25  
    26 
    27     return 0;
    28 
    29 }

     

     

     

    函数名: strdup

     能: 将串拷贝到新建的位置处

     法: char *strdup(char *str);

    程序例:

     

     1 #include <stdio.h>
     2 
     3 #include <string.h>
     4 
     5 #include <alloc.h>
     6 
     7  
     8 
     9 int main(void)
    10 
    11 {
    12 
    13     char *dup_str, *string = "abcde";
    14 
    15  
    16 
    17     dup_str = strdup(string);
    18 
    19     printf("%s
    ", dup_str);
    20 
    21     free(dup_str);
    22 
    23  
    24 
    25     return 0;
    26 
    27 }

     

     

    函数名: stricmp

     能: 以大小写不敏感方式比较两个串

     法: int stricmp(char *str1, char *str2);

    程序例:

     

     1 #include <string.h>
     2 
     3 #include <stdio.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char *buf1 = "BBB", *buf2 = "bbb";
    12 
    13     int ptr;
    14 
    15  
    16 
    17     ptr = stricmp(buf2, buf1);
    18 
    19  
    20 
    21     if (ptr > 0)
    22 
    23     printf("buffer 2 is greater than buffer 1
    ");
    24 
    25  
    26 
    27     if (ptr < 0)
    28 
    29     printf("buffer 2 is less than buffer 1
    ");
    30 
    31  
    32 
    33     if (ptr == 0)
    34 
    35     printf("buffer 2 equals buffer 1
    ");
    36 
    37  
    38 
    39     return 0;
    40 
    41 }

     

     

    函数名: strerror

     

     能: 返回指向错误信息字符串的指针

     

     法: char *strerror(int errnum);

     

    程序例:

     1 #include <stdio.h>
     2 
     3 #include <errno.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char *buffer;
    12 
    13     buffer = strerror(errno);
    14 
    15     printf("Error: %s
    ", buffer);
    16 
    17     return 0;
    18 
    19 }

     

     

    函数名: strcmpi

     能: 将一个串与另一个比较, 不管大小写

     法: int strcmpi(char *str1, char *str2);

    程序例:

     

     1 #include <string.h>
     2 
     3 #include <stdio.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char *buf1 = "BBB", *buf2 = "bbb";
    12 
    13     int ptr;
    14 
    15  
    16 
    17     ptr = strcmpi(buf2, buf1);
    18 
    19  
    20 
    21     if (ptr > 0)
    22 
    23     printf("buffer 2 is greater than buffer 1
    ");
    24 
    25  
    26 
    27     if (ptr < 0)
    28 
    29     printf("buffer 2 is less than buffer 1
    ");
    30 
    31  
    32 
    33     if (ptr == 0)
    34 
    35     printf("buffer 2 equals buffer 1
    ");
    36 
    37  
    38 
    39     return 0;
    40 
    41 }

     

     

    函数名: strncmp

     能: 串比较

     法: int strncmp(char *str1, char *str2, int maxlen);

    程序例:

     

     1 #include <string.h>
     2 
     3 #include <stdio.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9  
    10 
    11 {
    12 
    13     char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";
    14 
    15     int ptr;
    16 
    17  
    18 
    19     ptr = strncmp(buf2,buf1,3);
    20 
    21     if (ptr > 0)
    22 
    23     printf("buffer 2 is greater than buffer 1
    ");
    24 
    25     else
    26 
    27     printf("buffer 2 is less than buffer 1
    ");
    28 
    29  
    30 
    31     ptr = strncmp(buf2,buf3,3);
    32 
    33     if (ptr > 0)
    34 
    35     printf("buffer 2 is greater than buffer 3
    ");
    36 
    37     else
    38 
    39     printf("buffer 2 is less than buffer 3
    ");
    40 
    41  
    42 
    43     return(0);
    44 
    45 }

     

     

     

    函数名: strncmpi

     能: 把串中的一部分与另一串中的一部分比较, 不管大小写

     法: int strncmpi(char *str1, char *str2);

    程序例:

     

     1 #include <string.h>
     2 
     3 #include <stdio.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char *buf1 = "BBBccc", *buf2 = "bbbccc";
    12 
    13     int ptr;
    14 
    15  
    16 
    17     ptr = strncmpi(buf2,buf1,3);
    18 
    19  
    20 
    21     if (ptr > 0)
    22 
    23     printf("buffer 2 is greater than buffer 1
    ");
    24 
    25  
    26 
    27     if (ptr < 0)
    28 
    29     printf("buffer 2 is less than buffer 1
    ");
    30 
    31  
    32 
    33     if (ptr == 0)
    34 
    35     printf("buffer 2 equals buffer 1
    ");
    36 
    37  
    38 
    39     return 0;
    40 
    41 }

     

    函数名: strncpy

     能: 串拷贝

     法: char *strncpy(char *destin, char *source, int maxlen);

    程序例:

     

     1 #include <stdio.h>
     2 
     3 #include <string.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char string[10];
    12 
    13     char *str1 = "abcdefghi";
    14 
    15  
    16 
    17     strncpy(string, str1, 3);
    18 
    19     string[3] = ‘0‘;
    20 
    21     printf("%s
    ", string);
    22 
    23     return 0;
    24 
    25 }

     

     

     

    函数名: strnicmp

     能: 不注重大小写地比较两个串

     法: int strnicmp(char *str1, char *str2, unsigned maxlen);

    程序例:

     

     1 #include <string.h>
     2 
     3 #include <stdio.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char *buf1 = "BBBccc", *buf2 = "bbbccc";
    12 
    13     int ptr;
    14 
    15  
    16 
    17     ptr = strnicmp(buf2, buf1, 3);
    18 
    19  
    20 
    21     if (ptr > 0)
    22 
    23     printf("buffer 2 is greater than buffer 1
    ");
    24 
    25  
    26 
    27     if (ptr < 0)
    28 
    29     printf("buffer 2 is less than buffer 1
    ");
    30 
    31  
    32 
    33     if (ptr == 0)
    34 
    35     printf("buffer 2 equals buffer 1
    ");
    36 
    37  
    38 
    39     return 0;
    40 
    41 }

     

     

    函数名: strnset

     能: 将一个串中的所有字符都设为指定字符

     法: char *strnset(char *str, char ch, unsigned n);

    程序例:

     

     1 #include <stdio.h>
     2 
     3 #include <string.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char *string = "abcdefghijklmnopqrstuvwxyz";
    12 
    13     char letter = ‘x‘;
    14 
    15  
    16 
    17     printf("string before strnset: %s
    ", string);
    18 
    19     strnset(string, letter, 13);
    20 
    21     printf("string after  strnset: %s
    ", string);
    22 
    23  
    24 
    25     return 0;
    26 
    27 }

     

     

    函数名: strpbrk

     能: 在串中查找给定字符集中的字符

     法: char *strpbrk(char *str1, char *str2);

    程序例:

     

     1 #include <stdio.h>
     2 
     3 #include <string.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char *string1 = "abcdefghijklmnopqrstuvwxyz";
    12 
    13     char *string2 = "onm";
    14 
    15     char *ptr;
    16 
    17  
    18 
    19     ptr = strpbrk(string1, string2);
    20 
    21  
    22 
    23     if (ptr)
    24 
    25     printf("strpbrk found first character: %c
    ", *ptr);
    26 
    27     else
    28 
    29     printf("strpbrk didn‘t find character in set
    ");
    30 
    31  
    32 
    33     return 0;
    34 
    35 }

     

     

    函数名: strrchr

     能: 在串中查找指定字符的最后一个出现

     法: char *strrchr(char *str, char c);

    程序例:

     

     1 #include <string.h>
     2 
     3 #include <stdio.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char string[15];
    12 
    13     char *ptr, c = ‘r‘;
    14 
    15  
    16 
    17     strcpy(string, "This is a string");
    18 
    19     ptr = strrchr(string, c);
    20 
    21     if (ptr)
    22 
    23     printf("The character %c is at position: %d
    ", c, ptr-string);
    24 
    25     else
    26 
    27     printf("The character was not found
    ");
    28 
    29     return 0;
    30 
    31 }

     

     

    函数名: strrev

     能: 串倒转

     法: char *strrev(char *str);

    程序例:

     

     1 #include <string.h>
     2 
     3 #include <stdio.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char *forward = "string";
    12 
    13  
    14 
    15     printf("Before strrev(): %s
    ", forward);
    16 
    17     strrev(forward);
    18 
    19     printf("After strrev():  %s
    ", forward);
    20 
    21     return 0;
    22 
    23 }

     

     

    函数名: strset

     能: 将一个串中的所有字符都设为指定字符

     法: char *strset(char *str, char c);

    程序例:

     

     1 #include <stdio.h>
     2 
     3 #include <string.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char string[10] = "123456789";
    12 
    13     char symbol = ‘c‘;
    14 
    15  
    16 
    17     printf("Before strset(): %s
    ", string);
    18 
    19     strset(string, symbol);
    20 
    21     printf("After strset():  %s
    ", string);
    22 
    23     return 0;
    24 
    25 }
    26 
    27  
    28 
    29 函数名: strspn
    30 
    31 功 能: 在串中查找指定字符集的子集的第一次出现
    32 
    33 用 法: int strspn(char *str1, char *str2);
    34 
    35 程序例:
    36 
    37  
    38 
    39 #include <stdio.h>
    40 
    41 #include <string.h>
    42 
    43 #include <alloc.h>
    44 
    45  
    46 
    47 int main(void)
    48 
    49 {
    50 
    51     char *string1 = "1234567890";
    52 
    53     char *string2 = "123DC8";
    54 
    55     int length;
    56 
    57  
    58 
    59     length = strspn(string1, string2);
    60 
    61     printf("Character where strings differ is at position %d
    ", length);
    62 
    63     return 0;
    64 
    65 }

     

     

    函数名: strstr

     能: 在串中查找指定字符串的第一次出现

     法: char *strstr(char *str1, char *str2);

    程序例:

     

     1 #include <stdio.h>
     2 
     3 #include <string.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char *str1 = "Borland International", *str2 = "nation", *ptr;
    12 
    13  
    14 
    15     ptr = strstr(str1, str2);
    16 
    17     printf("The substring is: %s
    ", ptr);
    18 
    19     return 0;
    20 
    21 }

     

     

    函数名: strtod

     能: 将字符串转换为double型值

     法: double strtod(char *str, char **endptr);

    程序例:

     

     1 #include <stdio.h>
     2 
     3 #include <stdlib.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char input[80], *endptr;
    12 
    13     double value;
    14 
    15  
    16 
    17     printf("Enter a floating point number:");
    18 
    19     gets(input);
    20 
    21     value = strtod(input, &endptr);
    22 
    23     printf("The string is %s the number is %lf
    ", input, value);
    24 
    25     return 0;
    26 
    27 }

     

    函数名: strtok

     能: 查找由在第二个串中指定的分界符分隔开的单词

     法: char *strtok(char *str1, char *str2);

    程序例:

     

     1 #include <string.h>
     2 
     3 #include <stdio.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char input[16] = "abc,d";
    12 
    13     char *p;
    14 
    15  
    16 
    17     /* strtok places a NULL terminator
    18 
    19      in front of the token, if found */
    20 
    21     p = strtok(input, ",");
    22 
    23     if (p) printf("%s
    ", p);
    24 
    25  
    26 
    27     /* A second call to strtok using a NULL
    28 
    29      as the first parameter returns a pointer
    30 
    31      to the character following the token  */
    32 
    33     p = strtok(NULL, ",");
    34 
    35     if (p) printf("%s
    ", p);
    36 
    37     return 0;
    38 
    39 }

     

     

    函数名: strtol

     能: 将串转换为长整数

     法: long strtol(char *str, char **endptr, int base);

    程序例:

     

     1 #include <stdlib.h>
     2 
     3 #include <stdio.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char *string = "87654321", *endptr;
    12 
    13     long lnumber;
    14 
    15  
    16 
    17     /* strtol converts string to long integer  */
    18 
    19     lnumber = strtol(string, &endptr, 10);
    20 
    21     printf("string = %s  long = %ld
    ", string, lnumber);
    22 
    23  
    24 
    25     return 0;
    26 
    27 }

     

     

    函数名: strupr

     能: 将串中的小写字母转换为大写字母

     法: char *strupr(char *str);

    程序例:

     

     1 #include <stdio.h>
     2 
     3 #include <string.h>
     4 
     5  
     6 
     7 int main(void)
     8 
     9 {
    10 
    11     char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;
    12 
    13  
    14 
    15     /* converts string to upper case characters */
    16 
    17     ptr = strupr(string);
    18 
    19     printf("%s
    ", ptr);
    20 
    21     return 0;
    22 
    23 }

     

     

    函数名: swab

     能: 交换字节

     法: void swab (char *from, char *to, int nbytes);

    程序例:

     

     1 #include <stdlib.h>
     2 
     3 #include <stdio.h>
     4 
     5 #include <string.h>
     6 
     7  
     8 
     9 char source[15] = "rFna koBlrna d";
    10 
    11 char target[15];
    12 
    13  
    14 
    15 int main(void)
    16 
    17 {
    18 
    19     swab(source, target, strlen(source));
    20 
    21     printf("This is target: %s
    ", target);
    22 
    23     return 0;
    24 
    25 }

     

     

  • 相关阅读:
    How to Downgrade From iPhone Firmware 2.2 to 2.1 视频教程降级iphone 2.2 到2.1
    11种网站测试软件(转) 沧海一粟
    GoF23种设计模式之行为型模式之观察者模式
    Android柳叶刀之Button之图文并茂
    GoF23种设计模式之行为型模式之迭代器模式
    某大型银行深化系统之十九:日志规范
    GoF23种设计模式之行为型模式之中介者模式
    GoF23种设计模式之行为型模式之备忘录模式
    Android血刀之CheckBox之问卷调查
    Android应用开发之MetaData之数据挖掘
  • 原文地址:https://www.cnblogs.com/wangmengmeng/p/4555295.html
Copyright © 2011-2022 走看看